diff options
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | Q3a.cpp | 36 | ||||
-rw-r--r-- | Q3b.cpp | 29 | ||||
-rw-r--r-- | get_maximum.cpp | 0 | ||||
-rw-r--r-- | get_mean.cpp | 0 | ||||
-rw-r--r-- | get_minimum.cpp | 0 | ||||
-rw-r--r-- | mysqrt.cpp | 14 | ||||
-rw-r--r-- | statistics.h | 0 |
9 files changed, 83 insertions, 6 deletions
@@ -1,2 +1,6 @@ *.un~ +Q1 +Q2 +Q3a +Q3b @@ -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 @@ -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; +} @@ -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 @@ -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 |