Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Task Write the program sqrt which should calculate the square root of a number. Your program must not use the sqrt function in the

image text in transcribed
Programming Task Write the program sqrt which should calculate the square root of a number. Your program must not use the sqrt function in the math library! In fact, your program is not allowed to include math.h. Instead, your program should define its own function sgrt to calculate the square root of a given number. If you need any other functions in the math library (such as fabs) you need to define these by yourself The square root of a non-negative number n can be approximated by repeated calculation using the formula: NG = 0.5 ( LG + n / LG ) where NG means "next guess" and LG means "last guess". Your sqrt function should calculate the square root using this method. The initial guess will be the starting value of LG. Use an initial guess of 1.0. The function should calculate a value for NG using the above formula. The difference between NG and LG should be checked to see whether these two guesses are almost identical. If they are, NG should be accepted as the square root; otherwise, the new guess (NG) becomes the last guess (LG) and the process is repeated (another value is calculated for NG, the difference is checked, and so on). The loop should be repeated until the difference between NG and LG is less than 0.0000000001 Requirements 1. The program should repeatedly ask the user to enter a non-negative number to calculate its square root, or a negative number to 2. The program should call its sqrt function to calculate the square root of the number entered by the user. The function sqrt should return the square root to the calling function. The function sqrt must not do any input or output (such as with printf or scanf). 3The output should show the input number and its square root. The input number should be shown with 6 digits after the decimal point, the square root should be shown with 10 digits after the decimal point. Example: (user input is shown in bold) Enter a non-negative number (or a negative number to quit): 2 The square root of 2.000000 is approximately 1.4142135624 : Enter a non-negative number or a negative number to quit): 2.5 The square root of 2.500000 is approximately 1.5811388301 Enter a non-negative nunber (or a negative number to quit): 3.1415926 The square root of 3.141593 is approximately 1.7724538358 Enter a non-negative number (or a negative number to quit):4 The square root of 4.000000 is approximately 2.0000000000 : Enter a non-negative number or a negative number to quit): -1

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

Students also viewed these Databases questions

Question

LO14.5 Use the story line approach to presentations.

Answered: 1 week ago