Question: Carefully review the program and the documentation (comments) within. See if you can find the error and correct the program to always work. As is

Carefully review the program and the documentation (comments) within. See if you can find the error and correct the program to always work.

As is the program runs and compiles well with numbers above one, but anything between one and zero does not compile. Instead it loops(?), For instance, if I put 0.5 it will not output the answer. You can test it out yourself. Also when inputting one it does not put output the right square root of it. Also the program is for positive numbers only. Please help in finding a way to fix the program. Thank you. Also, if you could explain how you came to the idea of how to solve it; it would be helpful in understanding the problem. /* This program contains a function to compute square roots. It also shows how to generate random number in C++ */ #include  #include  #include  using namespace std; double squareroot(double x) { /* computes the square root of x */ /* make sure x is not negative .. no math crimes allowed! */ assert( x >= 0 ); if (x==0) return 0; /* the sqrt must be between xhi and xlo */ double xhi = x; double xlo = 0; double guess = x/2; /* We stop when guess*guess-x is very small */ while (abs(guess*guess-x) > 0.00001 ) { if (guess*guess > x) xhi = guess; else xlo = guess; guess = (xhi + xlo)/2; } return guess; } /* Test Stub */ int main() { double testvalue; cout << " Enter a TestValue= " ; cin >> testvalue; cout << endl; double testresult = squareroot(testvalue); cout << " Square Root= " << testresult << " " ; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!