Answered step by step
Verified Expert Solution
Question
1 Approved Answer
in c + + Write a program computes the square root of a number entered by the user. Note: while there is a math function
in c Write a program computes the square root of a number entered by the user.
Note: while there is a math function in the cmath library that computes a square root, do not use that function for this lab. Instead, write your own code to compute the square root using the algorithm described below.
The idea behind the algorithm is to repeatedly compute estimates of the squrare root until the desired accuracy is obtained. Each new estimate will be closer to the actual value of the square root. If x is the number for which the square root will be computed, the next estimate is previous estimate x previous estimate
To understand why this algorithm works, see Methods of Computing Square Roots.
The pseudocode for the algorithm is:
Get the number for which to compute the square root from the user x
Set the estimate of the square root to x initial estimate
As long the difference between the computed square root and the previously computed square root is less than the desired accuracy:
Set the previous estimate to the current estimate
Compute the next estimate: previous estimate x previous estimate
Output the final estimated square root to the screen with decimal places after the decimal point.
For example, if the user inputs:
the output would be
For this program, use a desired accuracy of or ea constant
If the user enters a value less than zero, output an error message:
The number must be greater than or equal to
Note: the abs function is available in the either the cmath or cstdlib library.
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