Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This lab exercise involves writing a program to calculate an approximation for the square root of a number entered by the user. In this assignment,

This lab exercise involves writing a program to calculate an approximation for the square root of a number
entered by the user. In this assignment, you are not allowed to call any library function for square root.
The program must output (in column format) the intermediate values that it uses in its calculations. After the
calculations are complete, the program must output the final result. (Refer to the Sample Output section of this
document for more details.)
The program must have two nested loops:
An outer loop that gets input values from the user and contains all of the calculations. This loop
repeats as long as the user keeps replying y or yes to the Keep running? prompt.
An inner loop that repeats the approximation calculations until the difference value is less than the
tolerance value.
This program must ask the user to input three values:
The number for which we need to calculate the square root. (Save this value in the inputNumber
variable.)
The tolerance(permissible error) for the calculated result.
The initial estimate for the square root.
The algorithm for calculating square root involves starting with an estimate for the square root. We do not
necessarily expect the initial estimate to very close to the real square root value, so we perform a calculation to
determine how bad the initial estimate is. Then we use this information to refine our estimate. We repeat the
process until we decide that the estimate is close enough.
Inputs from the user:
double inputNumber; //(we will calculate the square root of this number.)
double tolerance; // the acceptable error for the approximation.
double estimate; // an initial guess of the square root result
Variables used in the calculations:
double quotient; // inputNumber / estimate
double difference; // difference between estimate and quotient.
int n; // number of guesses so far (loop counter).
Outputs to the screen:
For each pass through the inner loop, output the intermediate values of the variables used in the
calculations, formatted in columns. (See also the Sample Output section of this document.)
After the loop exits, output the final result.
Calculations:
The program must have a do-while loop that performs the following steps:
Calculate: quotient = inputNumber / estimate.
Calculate: difference = fabs(estimate quotient).
(The fabs function is part of the cmath library.)
Output: The intermediate values of n, estimate, quotient, and
difference.
Calculate: a new value for estimate: the average between the previous value of
estimate and the quotient.
The loop continues until the difference is less than the tolerance.
Before the beginning of the inner loop, output column headings for the intermediate values. Use these column
headings:
n estimate quotient difference
___________________________Sample Output
In the sample output shown below, text that the user types is shown in BOLD font. When the program actually
runs, all text is shown in the same font.)
Sample Input / Output
Enter a floating point number: 10000
Enter the desired tolerance value: 0.0001
Enter the initial estimate: 5
tolerance =0.00010000000000000000
Estimating square root of 10000.000000000000000000
The square root of 10000.000000000000000000
is ,100.000000001492139745
(+/-0.000100000000000000
image text in transcribed

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

What are some of the possible scenes from our future?

Answered: 1 week ago