Question: The Babylonian algorithm to compute the square root of a positive number n is as follows: a) Make a guess at the answer (you can

The Babylonian algorithm to compute the square root of a positive number n is as follows: a) Make a guess at the answer (you can pick n/2 as initial guess)

b) Compute r = n/guess

c) Set guess = (guess + r)/2

d) Go back 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 to the console. Your program should also use the sqrt function from the cmath 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 = 5 as input, and make an initial guess at the answer as 2, 2.5, and 3 (i.e., show 3 different runs of your code). Use the shell below as your starting code and add in your code inside the function. #include #include using namespace std; double takeInput();

double squareRoot(double userInput);

double calculatePctError(double approximation, double userInput);

void outputValues(double approximation, double userInput, double pctError); int main() { double input, guess, pctError; input = takeInput(); guess = squareRoot(input); pctError = calculatePctError(guess, input); outputValues(input, guess, pctError); return 0; } //Function that takes user input to calculate an approximate square root. double takeInput()

{ //Your code goes here } //Function that calculates an approximation of a square root using the Babylonian method. double squareRoot(double userInput)

{ //Your code goes here } //Function that calculates the percent error in the approximation. double calculatePctError(double approximation, double userInput)

{ //Your code goes here } //Function that outputs the approximation and actual square root values and percent error value. void outputValues(double approximation, double userInput, double pctError) { //Your code goes here }

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!