diff options
author | Matthew Strapp <msattr@gmail.com> | 2019-02-20 16:00:41 -0600 |
---|---|---|
committer | Matthew Strapp <msattr@gmail.com> | 2019-02-20 16:00:41 -0600 |
commit | 93ff5f30e854390dc6bb790414e13986144514fa (patch) | |
tree | 3ae048da5dfafaa05f6573fd8a29a402a38a0470 /ee1301/wk3/lab3/RootBabylonOG.cpp | |
parent | (Screams internally) (diff) | |
download | homework-93ff5f30e854390dc6bb790414e13986144514fa.tar homework-93ff5f30e854390dc6bb790414e13986144514fa.tar.gz homework-93ff5f30e854390dc6bb790414e13986144514fa.tar.bz2 homework-93ff5f30e854390dc6bb790414e13986144514fa.tar.lz homework-93ff5f30e854390dc6bb790414e13986144514fa.tar.xz homework-93ff5f30e854390dc6bb790414e13986144514fa.tar.zst homework-93ff5f30e854390dc6bb790414e13986144514fa.zip |
Finish Lab 3
Diffstat (limited to 'ee1301/wk3/lab3/RootBabylonOG.cpp')
-rw-r--r-- | ee1301/wk3/lab3/RootBabylonOG.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/ee1301/wk3/lab3/RootBabylonOG.cpp b/ee1301/wk3/lab3/RootBabylonOG.cpp new file mode 100644 index 0000000..07c1951 --- /dev/null +++ b/ee1301/wk3/lab3/RootBabylonOG.cpp @@ -0,0 +1,32 @@ +#include <iostream> +#include <cmath> +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; + } +} +int main () { + double temp, temp2; + cout << "Enter the Number to find square root: "; + cin >> temp; + if (temp<=0) { + cout << "Please enter a valid input(Positive Integer)." << endl; + return sqrt(-1); + } + cout << "Guessing..." << endl; + double real=sqrt(temp); + temp2=temp; + temp = gennewGuess(temp, 1.0); + cout << "The Final Guess: " << temp << endl + << "Actual Value: " << real << endl; +} |