Answered step by step
Verified Expert Solution
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 tolerancepermissible 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 dowhile loop that performs the following steps:
Calculate: quotient inputNumber estimate.
Calculate: difference fabsestimate 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:
Enter the desired tolerance value:
Enter the initial estimate:
tolerance
Estimating square root of
The square root of
is
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started