Answered step by step
Verified Expert Solution
Link Copied!

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))/2.
(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 /2.0(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))/2
Output the final estimated square root to the screen with 6 decimal places after the decimal point.
For example, if the user inputs:
2.0
the output would be
1.414214
For this program, use a desired accuracy of 0.000001 or 1e-6(a constant).
If the user enters a value less than zero, output an error message:
The number must be greater than or equal to 0.
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

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

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions

Question

define what is meant by the term human resource management

Answered: 1 week ago