From 1c4d830764f3b205463e7901b90a9c009850f44b Mon Sep 17 00:00:00 2001 From: RossTheRoss Date: Thu, 21 Feb 2019 19:51:53 +0000 Subject: Fix my problems because I'm an idiot --- ee1301/wk3/lab3/RootBabylonOG.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'ee1301/wk3/lab3') diff --git a/ee1301/wk3/lab3/RootBabylonOG.cpp b/ee1301/wk3/lab3/RootBabylonOG.cpp index 3abdd36..9972250 100644 --- a/ee1301/wk3/lab3/RootBabylonOG.cpp +++ b/ee1301/wk3/lab3/RootBabylonOG.cpp @@ -3,7 +3,7 @@ using namespace std; double gennewGuess(double n, double old_guess) { - double new_guess = ((old_guess+n) / old_guess) / 2; + double new_guess = ( old_guess + ( n / old_guess ) ) / 2; return new_guess; } @@ -13,7 +13,7 @@ double BabylonRoot(double n) { do { guess = gennewGuess(n,guess); cout << guess << endl; - } while ((guess-real)/real>0.01); //Keep guessing unitl within 1% of the actual square root + } while (abs(guess-real)/real>0.05); //Keep guessing unitl within 5% of the actual square root return guess; } @@ -23,11 +23,12 @@ int main () { cin >> n; if (n<=0) { cout << "Please enter a valid input(Positive Integer)." << endl; - return sqrt(-1); + return 2; } cout << "Guessing..." << endl; double real=sqrt(n); guess = BabylonRoot(n); cout << "The Final Guess: " << guess << endl << "Actual Value: " << real << endl; -} + return 0; +} \ No newline at end of file -- cgit v1.2.3