summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Makefile0
-rwxr-xr-xQ1bin0 -> 10160 bytes
-rw-r--r--Q1.cpp28
-rw-r--r--Q2.cpp0
-rw-r--r--mysqrt.cpp21
-rw-r--r--mysqrt.h9
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
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Makefile
diff --git a/Q1 b/Q1
new file mode 100755
index 0000000..5691942
--- /dev/null
+++ b/Q1
Binary files 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 <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/Q2.cpp b/Q2.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Q2.cpp
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