Question: Please use C++ visual studio /* Program to demonstrate determining the square root of a number using Babylonian method implemented using a function with a

Please use C++ visual studioPlease use C++ visual studio /* Program to demonstrate determining the square

root of a number using Babylonian method implemented using a function with

/* 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

root. ";

cin >> numb;

newguess = 0.5*numb;

// call function using recursion

ansrecur = recursquareroot(numb, newguess);

cout

//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)

{

ans = g1r;

}

else

{

ans = recursquareroot(nr, g1r);

}

return ans;

Labs 4-7 Makeup CMPSC 201 - Fall 2017 Due by the end of your recitation section on 10/27/17 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. Remember to write your function definition after main and write a function declaration (prototype) before main for your function. Write comments immediately before your new function describing the task of the function, input, output, and processing for the second function. Do not use the same variable name in multiple (more than one) functions. Do not use any concepts beyond Chapter 6 of your textbook! Remember your problem description in your introductory comments should be detailed enough so anyone reading it will know what the code should be doing without accessing any outside documentation. Do not skip on the details. Attach your C++ source file to the Assignment on CANVAS Remember to confirm all your submissions to CANVAS

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!