The Babylonian algorithm to compute the square root of a positive number n is as
ID: 3783401 • Letter: T
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 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<iostream> #include<cmath> 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 }
Explanation / Answer
#include<iostream>
#include<cmath>
//for displaying desired number of degits after decimal point ,use setprecission
#include<iomanip>
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( guess, input, pctError);
return 0;
}
//Function that takes user input to calculate an approximate square root. double takeInput()
double takeInput()
{ //Your code goes here
double input;
cin >> input;
return input;
}
//Function that calculates an approximation of a square root using the Babylonian method.
double squareRoot(double userInput)
{ //Your code goes here
double x = userInput;
double y = 1;
double e = 0.000001; /* e decides the accuracy level*/
while (x - y > e)
{
x = (x + y) / 2;
y = userInput / x;
}
return x;
}
//Function that calculates the percent error in the approximation. double calculatePctError(double approximation, double userInput)
double calculatePctError(double approximation, double userInput)
{ //Your code goes here
double error = 0 ;
double root = sqrt(userInput);
error = abs(approximation - root) / root * 100;
return error;
}
//Function that outputs the approximation and actual square root values and percent error value. void outputValues(double approximation, double userInput, double pctError)
void outputValues(double approximation, double userInput, double pctError)
{ //Your code goes here
cout << "approximation = " << std::setprecision(16)<<approximation << endl;
cout << "userInput = " << userInput << endl;
cout << "Actual square root = " << sqrt(userInput) << endl;
cout << "pctError = " << pctError << endl;
}
---------------------------------------------------------------------------------------------------
output1:
4
approximation = 2.000000092922295
userInput = 4
Actual square root = 2
pctError = 4.6461147373833e-006
output2:
25
approximation = 5.000000000053722
userInput = 25
Actual square root = 5
pctError = 1.074447197879636e-009
output3:
5
approximation = 2.236067977499978
userInput = 5
Actual square root = 2.23606797749979
pctError = 8.420755847814751e-012
output4:
15
approximation = 3.872983698008724
userInput = 15
Actual square root = 3.872983346207417
pctError = 9.083470692981591e-006
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.