Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a C++ problem. Please give full explanation and code in details. Thanks. The ancient Babylonians produced a remarkably efficient process to compute the

image text in transcribed

This is a C++ problem. Please give full explanation and code in details. Thanks.

The ancient Babylonians produced a remarkably efficient process to compute the square root of any number n. The method is referred to as a "guess and check" algorithm because it involves guessing the answer and then checking to see if the guess produces the correct answer. The "trick" is how to (systematically) improve the guess if it isn't close enough, in order to make a better guess the next time. The simple intuition that the Babylonians discovered is to make the next (improved) guess equal to the average of the old guess and the ratio of n to the old guess new guess -(oldguess + (n/old guess))/2 This algorithm converges quickly, which means that if we begin with any arbitrary guess and update it a sufficient number of times, we will obtain a very good approximation of the square root of the value. In fact, this method converges quadratically and will produce excellent square root approximations in a small number of iterations The algorithm consists of three simple steps 1. Make an initial guess at the answer (start with 1 or n/2) 2. Revise the guess: new guess -(old guess + n/ old guess )/2 3. Determine if the guess is sufficiently close to the square root of n. In this problem we will say the guess is sufficiently close if the new guess is within 1% of the old guess. If it is not, repeat Step 2 for a sufficient number of iterations until that criterion is satisfied. The more that Step 2 is repeated, the closer the guess will become to the square root of n Write a program that has the user input a positive integer for n, and then uses a while-loop to iterate through the Babylonian algorithm until the stopping criteria in Step 3 is met. Output the result as a real number (i.e use doubles to store the new and old guesses) Your program must do the following Prompt the user to input a positive integer value Validate the input (i.e., check that it is nonnegative). Notify the user and terminate the program if an invalid value is entered For each iteration of the algorithm loop, output the value of the guess. (This is a useful debugging technique and will help you understand what your code is doing!) Output the square root of the value as determined by the Babylonian algorithm. For the purpose of comparison, output the square root of the value as determined by the cmath function sqrt). 1. 2. 3. 4. 5. Demonstrate your program to a TA using the following input (at a minimum) 25 42

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

Pro PowerShell For Database Developers

Authors: Bryan P Cafferky

1st Edition

1484205413, 9781484205419

More Books

Students also viewed these Databases questions