Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I know how to do it from scratch, but I am having a lot of trouble with the code

ID: 3783383 • Letter: I

Question

I know how to do it from scratch, but I am having a lot of trouble with the code that my teacher has provided me to use.

1.(40 pts) The Babylonian algorithm to compute the square root of a positive number nis as follows: a) Make a guess at the answer (you can pick n/2as initial guess) b) Computera n/guess c) Set guess (guess r)/2 d) Goback to step b) for as many iterations as necessary. The more times steps b) and c are repeated, the closer guess will become to the square root of n. Write a program that inputs a double for n from the console, iterates through the Babylonian algorithm five times, and outputs the answer as a double to two decimal places tothe console. Your program should also use the sqrt function from the camath library to calculate the actual square root of the user input and calculate a error as follows: Error abs(guess actual /actual 100. Use the abs function from the same library to calculate the absolute value of the difference and output it to two decimal places. Experiment with several inputs and starting values for the guess to test your program for correctness. Turn in console output text results that use n i5 as input, and make an initial guess at the answer as 2,2.5, and 3 (ie., show 3 different runs of your code) Use the shell below as your starting code and add in your code inside the function. Eincludekios tream> #include

Explanation / Answer

#include <iostream>

#include <cmath>

using namespace std;

double takeinput();

double sqroot(double input);

double cal_err(double approx, double userInput);

//void output(double approx, double userInput,double err);

double answer_n,inp, guess=2, r;

int main()

{

//double answer_n,inp, guess=2, r;

inp=takeinput();

guess=sqroot(inp);

double err=cal_err(guess,inp);

//output(guess,inp,err);


return 0;

}

double takeinput()

{

    int n;

   cout << "This program will compute the square root ";

   cout << "of a number using the Babylonian algorithm. ";
  
cout << "Please enter a whole number and press the return key: ";
  
cin >> n;

cout << "The sqaure root of "<< inp << " using algorithm is " << guess;

cout << " error is " << err;


return n;

}

double sqroot(double inp)

{

int n, count(10);
double r;

r = inp/guess;

guess = (guess + r)/2;


while (count > 0)

{

r = inp/guess;

guess = (guess + r)/2;


if (guess <= (guess * 0.01) + guess)

answer_n = guess;

else

r = inp/guess;

guess = (guess + r)/2;


count-=1;

}


return answer_n;


}


double cal_err(double guess,double inp)

{


double actual=pow(inp,0.5);

double err= abs(guess-actual)/actual*100;

return err;

}

/*output(double guess, double inp,double err)


{

cout << "The sqaure root of "<< inp << " using algorithm is " << guess;

cout << " error is " << err;


return;


} */