Zookal
Zookal

We’d love to hear any feedback or comment from you!

© 2011-2021 Zookal Pty Ltd

View question and answer

From our collection of questions and answers
Engineering · Computer Science
Question details

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 <iostream>
#include <cmath>
#include <cassert>

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 << "\n Enter a TestValue= " ; 
  cin >> testvalue;
  cout << endl;
  double testresult = squareroot(testvalue);
  cout << "\n Square Root= " << testresult << "\n" ; 
  }
Answer
Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.