From d689dad2a36e74cd8db73cedbecf683cf80bcede Mon Sep 17 00:00:00 2001 From: RossTheRoss Date: Thu, 21 Feb 2019 15:11:06 +0000 Subject: Split functions to do the same thing --- ee1301/wk3/lab3/RootBabylonOG.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'ee1301/wk3/lab3') 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; } -- cgit v1.2.3