Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

What is an optimal portfolio? Why is it important?

Answered: 1 week ago