summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore4
-rw-r--r--Makefile6
-rw-r--r--Q3a.cpp36
-rw-r--r--Q3b.cpp29
-rw-r--r--get_maximum.cpp0
-rw-r--r--get_mean.cpp0
-rw-r--r--get_minimum.cpp0
-rw-r--r--mysqrt.cpp14
-rw-r--r--statistics.h0
9 files changed, 83 insertions, 6 deletions
diff --git a/.gitignore b/.gitignore
index a60d365..de9d66d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,6 @@
*.un~
+Q1
+Q2
+Q3a
+Q3b
diff --git a/Makefile b/Makefile
index a48cf1d..0eafcb5 100644
--- a/Makefile
+++ b/Makefile
@@ -1,3 +1,9 @@
Q1: Q1.cpp mysqrt.cpp mysqrt.h
g++ -o Q1 mysqrt.cpp Q1.cpp
+
+Q3a: Q3a.cpp
+ g++ -o Q3a Q3a.cpp
+
+Q3b: Q3b.cpp
+ g++ -o Q3b Q3b.cpp
diff --git a/Q3a.cpp b/Q3a.cpp
new file mode 100644
index 0000000..cbe9023
--- /dev/null
+++ b/Q3a.cpp
@@ -0,0 +1,36 @@
+
+#include <iostream>
+
+using namespace std;
+
+int gcd(int m, int n);
+
+int gcd(int m, int n)
+{
+ if (m <= 0 || n <= 0)
+ return 0;
+
+ if (m % n == 0)
+ return n;
+ else
+ return gcd(n, m%n);
+}
+
+int main(int argc, const char *argv[])
+{
+ int m, n;
+ int result;
+ cout << "Enter 2 numbers to calculate the greatest common divisor (0 to quit)" << endl;
+ do {
+ cout << ">> ";
+ cin >> m >> n;
+ if (m <= 0 || n <= 0) {
+ cout << "Please input positive numbers (0 to quit)" << endl;
+ } else {
+ result = gcd(m, n);
+ cout << "gcd(" << m << ", " << n << ") = " << result << endl;
+ }
+ } while (m != 0 || n != 0);
+
+ return 0;
+}
diff --git a/Q3b.cpp b/Q3b.cpp
new file mode 100644
index 0000000..08676c8
--- /dev/null
+++ b/Q3b.cpp
@@ -0,0 +1,29 @@
+
+#include <iostream>
+
+using namespace std;
+
+double series(int n);
+
+double series(int n)
+{
+ if (n <= 0)
+ return 0;
+ else
+ return (double) (n/(2.0*n+1.0)) + series(n-1);
+}
+
+int main(int argc, const char *argv[])
+{
+ int x;
+ cout << "enter a number to see the sum of the series (0 to quit)" << endl;
+ do {
+ cout << ">> ";
+ cin >> x;
+ if (x <= 0)
+ cout << "Please enter a positive number (0 to quit)" << endl;
+ else
+ cout << "series(" << x << ") = " << series(x) << endl;
+ } while (x != 0);
+ return 0;
+}
diff --git a/get_maximum.cpp b/get_maximum.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/get_maximum.cpp
diff --git a/get_mean.cpp b/get_mean.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/get_mean.cpp
diff --git a/get_minimum.cpp b/get_minimum.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/get_minimum.cpp
diff --git a/mysqrt.cpp b/mysqrt.cpp
index 088fdde..18bb632 100644
--- a/mysqrt.cpp
+++ b/mysqrt.cpp
@@ -5,18 +5,20 @@ using namespace std;
double mysqrt(double x)
{
bool goodenough = false;
- double guess;
- double last = 0.0;
+ double lastGuess, nextGuess;
+
+ lastGuess = x / 10.0;
if (x <= 0)
return x;
while (!goodenough) {
- guess = (last + (x / last)) / 2.0;
- last = guess;
+
+ nextGuess = (lastGuess + (x / lastGuess)) / 2.0;
+ lastGuess = nextGuess;
- goodenough = abs(guess - last) <= 0.000000001;
+ goodenough = abs(nextGuess - lastGuess) <= 0.000000000001;
}
- return guess;
+ return nextGuess;
}
diff --git a/statistics.h b/statistics.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/statistics.h