Question: C++ CODE There is a Babylonian Method for calculating a square root of a number. In this method you have the number for which you
C++ CODE
There is a Babylonian Method for calculating a square root of a number. In this method you have the number for which you want to find the square root and an original guess. Then you continue to calculate new guesses using the formula newGuess = (lastGuess + (n/lastGuess))/2 where n is the number for which you are calculated the square root and lastGuess is the previous guess. The calculation of new guesses continues until difference between guesses is less than a specified value.
A template with the Babylonian Method implemented with a recursive function is posted below.
-------------------------------------
/* Program to demonstrate determining the square root of a number using Babylonian method implemented using a function with a loop and a recursive function. The Babylonian method starts with a guess for the root and then generates new guesses using the formula
newGuess = (lastGuess + (n/lastGuess))/2
where n is the number.
input: n (the number for which the square root is needed)
output: the square root of n or a number close to the square root of n
processing: start out with new guess being 1/2 of the number, then keep generating new guesses using the previous new guess as last guess until the difference between the new guess and last guess is less than 0.0000001 */
#include
# include
# include
using namespace std;
double recursquareroot(double nr, double g1r);
int main()
{
double numb, newguess, ansloop, ansrecur;
cout << "Enter the number for which you want to find the square
root. ";
cin >> numb;
newguess = 0.5*numb;
// call function using recursion
ansrecur = recursquareroot(numb, newguess);
cout << "The answer using a loop recursion is " << ansrecur << endl
<< endl;
//call function using loop
return 0;
}
//Add comments and function to use a loop rather than recursion
/* Function that uses a recursive function to implement the Babylonian
method
to determine the square root.
newGuess = (lastGuess + (n/lastGuess))/2
where n is the number.
input: the number and the first guess for the square root will be passed
to the function
using value paramters.
output: the value of new guess when the difference is less than
0.0000001
processing: Use a recursive function that will keep calling a function to
generate a
newGuess until the difference between newGuess and lastGuess
is less than 0.0000001
*/
double recursquareroot(double nr, double g1r)
{
double g2r = g1r;
double ans;
g1r = (g2r + (nr / g2r)) / 2;
if (fabs(g2r - g1r) <= 0.0000001)
{
ans = g1r;
}
else
{
ans = recursquareroot(nr, g1r);
}
return ans;
}
----------------------------
Add a new function that uses repetition rather than recursion to calculate the square root using the Babylonian method. (Remember to write comments for your function prior to writing any code for your function). Call this new function from main and output the result of this new function within main.
Please do not use functions or arrays as we have not learned them yet!
Thanks for the help!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
