Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Goal: practicing functions and recursion Problem: There is a Babylonian Method for calculating a square root of a number. In this method you have the

Goal: practicing functions and recursion Problem: 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 on Canvas. Download and review this template. 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.

Template:

/* Program to demonstrate determining the square root of a number using

Babylonian method implemented using a function with a loop and a

recursive function.

Th 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;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Using Entity Relationship Diagrams

Authors: Sikha Saha Bagui, Richard Walsh Earp

3rd Edition

103201718X, 978-1032017181

More Books

Students also viewed these Databases questions