Babylonian Algorithm with C++ unable to get loop to run -
is refusal program produce result consequence of unable loop?
please find below code more information.
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { double x, squareroot, oldsquareroot, counter=0; cout << "enter positive number: " ; cin >> x ; { (oldsquareroot = x/2; abs(squareroot-oldsquareroot) >= 1e-9 && abs(squareroot - oldsquareroot)>=(1e-8*x);counter++ ) { squareroot = (squareroot+x/squareroot)/2; } } cout << setprecision(15); cout << "the value of squareroot is:" << squareroot << endl; cout << "the number of interations required computation is:" << counter << endl; return 0; }
there mistakes. works fine.
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { double x, squareroot, oldsquareroot, counter = 0; cout << "enter positive number: "; cin >> x; { squareroot = x; // fixed error 1: using of uninitialized variable. (oldsquareroot = x / 2; abs(squareroot - oldsquareroot) >= 1e-9 && abs(squareroot - oldsquareroot) >= (1e-8*x); counter++) { oldsquareroot = squareroot;// fixed eror 2: oldsquareroot not changed in loop , can't exit. squareroot = (squareroot + x / squareroot) / 2; } } cout << setprecision(15); cout << "the value of squareroot is:" << squareroot << endl; cout << "the number of interations required computation is:" << counter << endl; return 0; }
i hope you.
Comments
Post a Comment