From 8932ac3c9881f800310e03fb147dce3261989eca Mon Sep 17 00:00:00 2001 From: Michael Abed Date: Mon, 27 Feb 2012 11:12:18 -0500 Subject: initial commit --- .gitignore | 2 ++ Makefile | 0 Q1 | Bin 0 -> 10160 bytes Q1.cpp | 28 ++++++++++++++++++++++++++++ Q2.cpp | 0 mysqrt.cpp | 21 +++++++++++++++++++++ mysqrt.h | 9 +++++++++ 7 files changed, 60 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100755 Q1 create mode 100644 Q1.cpp create mode 100644 Q2.cpp create mode 100644 mysqrt.cpp create mode 100644 mysqrt.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a60d365 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +*.un~ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/Q1 b/Q1 new file mode 100755 index 0000000..5691942 Binary files /dev/null and b/Q1 differ diff --git a/Q1.cpp b/Q1.cpp new file mode 100644 index 0000000..6b23316 --- /dev/null +++ b/Q1.cpp @@ -0,0 +1,28 @@ + +#include +#include + +#include "mysqrt.h" + +using namespace std; +int main() +{ + double input; + double sq, mysq; + + do { // + cout << "Enter a number (negative to quit)" << endl; + cout << ">> "; + cin >> input; + if (input >= 0) { + mysq = mysqrt(input); + sq = sqrt(input); + cout << "mysqrt(" << input << ") = " << mysq << endl; + cout << "sqrt(" << input << ") = " << sq << endl; + cout << "sqrt and mysqrt differ by " << abs(sq - mysq) << endl; + } + + } while (input >= 0); + + return 0; +} diff --git a/Q2.cpp b/Q2.cpp new file mode 100644 index 0000000..e69de29 diff --git a/mysqrt.cpp b/mysqrt.cpp new file mode 100644 index 0000000..29fedf2 --- /dev/null +++ b/mysqrt.cpp @@ -0,0 +1,21 @@ + +#include "mysqrt.h" + +using namespace std; +double mysqrt(double x) +{ + bool goodenough = false; + double guess; + double last = x / 5.0; + + if (x <= 0) + return x; + + while (!goodenough) { + guess = (last + (x / last)) / 2.0; + last = guess; + + goodenough = abs(guess - last) <= 0.000000001; + } + return guess; +} diff --git a/mysqrt.h b/mysqrt.h new file mode 100644 index 0000000..459cc99 --- /dev/null +++ b/mysqrt.h @@ -0,0 +1,9 @@ + +#ifndef MYSQRT_H +#define MYSQRT_H + +#include + +double mysqrt(double x); + +#endif -- cgit v1.2.3-54-g00ecf