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

C++ CODE There is a Babylonian Method for calculating a square root of a number.

ID: 3602790 • Letter: C

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 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 <iostream>

# include <cmath>

# include <iomanip>

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!

Explanation / Answer

Please note the below program has been tested on ubuntu 16.04 system and compiled using g++ compiler. This code will also work on code blocks.

-----------------------------------------------------------------------------------------------------------------------------------

Program:

------------------------------------------------------------------------------------------------------------------------------------

#include<iostream>

using namespace std;

//Function Prototype
float squareRoot(float num);

//Starting of main Function

int main()

{

//Data type declaration

float num;

float guess;

cout<<"Enter a number: ";

cin>>num;

//Printing the result

cout<<"The Square root of "<<num<<" is "<< squareRoot(num)<<endl;
}

//Function Defination

float squareRoot(float num)
{

float guess;

int count =1;

cout<<"Enter a starting guess: ";

cin>>guess;

float x = guess ;

float y = 1;

//Here e decides the accuracy level

float e = 0.000001;

while(x - y > e)

{
   x = (x + y)/2;

   cout<<"step "<<count<<"     :      "<<x<<endl;

   y = num/x;

   count++;
}

return x;
}