diff options
author | Michael Abed <michaelabed@gmail.com> | 2012-02-27 11:12:18 -0500 |
---|---|---|
committer | Michael Abed <michaelabed@gmail.com> | 2012-02-27 11:12:18 -0500 |
commit | 8932ac3c9881f800310e03fb147dce3261989eca (patch) | |
tree | 9336e291f283694c328933fb3ba00e6baa53acc9 | |
download | ec327-lab2-8932ac3c9881f800310e03fb147dce3261989eca.tar.gz ec327-lab2-8932ac3c9881f800310e03fb147dce3261989eca.tar.bz2 ec327-lab2-8932ac3c9881f800310e03fb147dce3261989eca.zip |
initial commit
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 0 | ||||
-rwxr-xr-x | Q1 | bin | 0 -> 10160 bytes | |||
-rw-r--r-- | Q1.cpp | 28 | ||||
-rw-r--r-- | Q2.cpp | 0 | ||||
-rw-r--r-- | mysqrt.cpp | 21 | ||||
-rw-r--r-- | mysqrt.h | 9 |
7 files changed, 60 insertions, 0 deletions
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 Binary files differnew file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ + +#include <iostream> +#include <cmath> + +#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/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 <cmath> + +double mysqrt(double x); + +#endif |