From 973562e3bbffbe2502649ce30d55e7cc6cbc6c3c Mon Sep 17 00:00:00 2001 From: Michael Abed Date: Fri, 2 Mar 2012 14:19:26 -0500 Subject: i did things --- .gitignore | 4 ++++ Makefile | 6 ++++++ Q3a.cpp | 36 ++++++++++++++++++++++++++++++++++++ Q3b.cpp | 29 +++++++++++++++++++++++++++++ get_maximum.cpp | 0 get_mean.cpp | 0 get_minimum.cpp | 0 mysqrt.cpp | 14 ++++++++------ statistics.h | 0 9 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 Q3a.cpp create mode 100644 Q3b.cpp create mode 100644 get_maximum.cpp create mode 100644 get_mean.cpp create mode 100644 get_minimum.cpp create mode 100644 statistics.h 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 + +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 + +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 diff --git a/get_mean.cpp b/get_mean.cpp new file mode 100644 index 0000000..e69de29 diff --git a/get_minimum.cpp b/get_minimum.cpp new file mode 100644 index 0000000..e69de29 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 -- cgit v1.2.3