aboutsummaryrefslogtreecommitdiffstats
path: root/ee1301/wk3/lab3
diff options
context:
space:
mode:
authorRossTheRoss <msattr@gmail.com>2019-02-21 15:11:06 +0000
committerRossTheRoss <msattr@gmail.com>2019-02-21 15:11:06 +0000
commitd689dad2a36e74cd8db73cedbecf683cf80bcede (patch)
tree49a501f6471f46ffc32b6a3dbbd71a5f9d939392 /ee1301/wk3/lab3
parentForget to commit things (diff)
downloadhomework-d689dad2a36e74cd8db73cedbecf683cf80bcede.tar
homework-d689dad2a36e74cd8db73cedbecf683cf80bcede.tar.gz
homework-d689dad2a36e74cd8db73cedbecf683cf80bcede.tar.bz2
homework-d689dad2a36e74cd8db73cedbecf683cf80bcede.tar.lz
homework-d689dad2a36e74cd8db73cedbecf683cf80bcede.tar.xz
homework-d689dad2a36e74cd8db73cedbecf683cf80bcede.tar.zst
homework-d689dad2a36e74cd8db73cedbecf683cf80bcede.zip
Split functions to do the same thing
Diffstat (limited to '')
-rw-r--r--ee1301/wk3/lab3/RootBabylonOG.cpp24
1 files changed, 13 insertions, 11 deletions
diff --git a/ee1301/wk3/lab3/RootBabylonOG.cpp b/ee1301/wk3/lab3/RootBabylonOG.cpp
index e97ff49..5e73234 100644
--- a/ee1301/wk3/lab3/RootBabylonOG.cpp
+++ b/ee1301/wk3/lab3/RootBabylonOG.cpp
@@ -3,18 +3,20 @@
using namespace std;
double gennewGuess(double n, double old_guess) {
- double real, old;
- real=sqrt(n);
- double new_guess = ( old_guess + n / old_guess ) / 2;
- old=new_guess;
- if (((new_guess-real)/real)>0.01) { //This will be true if the difference between the real root and guess is more than 1%.
- cout << old << endl;
- return gennewGuess(n, old);
- } else {
- cout << old << endl;
- return new_guess;
+ double new_guess = (old_guess+n/old_guess)/2;
+ return new_guess;
}
+
+double BabylonRoot(double n) {
+ double real, guess=1; //Initial guess is set to 1
+ real=sqrt(n);
+ do {
+ guess = gennewGuess(n,guess);
+ cout << guess << endl;
+ } while ((guess-real)/real>0.01);
+ return guess;
}
+
int main () {
double temp;
cout << "Enter the Number to find square root: ";
@@ -25,7 +27,7 @@ int main () {
}
cout << "Guessing..." << endl;
double real=sqrt(temp);
- temp = gennewGuess(temp, 1.0);
+ temp = BabylonRoot(temp);
cout << "The Final Guess: " << temp << endl
<< "Actual Value: " << real << endl;
}