Question: (20 pts) Given an integer, a, one can approximate its square root as follows: start with a very rough guess about the answer, x0, and

 (20 pts) Given an integer, a, one can approximate its square

(20 pts) Given an integer, a, one can approximate its square root as follows: start with a very rough guess about the answer, x0, and then improve the guess using the following formula: x1=(x0+a/x0)/2 For example, if we want to find the square root of 9 , and we start with x0=6, then x1=(6+9/6)/2=15/4=3.75, which is closer. We can repeat the procedure, using x1 to calculate x2, and so on. In this case, x2=3.075 and x3=3.00091. So that is converging very quickly on the right answer (which is 3 ). Write a C program (in file q1.c) that takes an integer as a command line argument and prints an approximation (as a float) of the square root of the argument, using this algorithm. You should not use the sqrt() function from the math.h library. As your initial guess, use a/2. Your program should iterate until it gets two consecutive estimates that differ by less than 0.0001; in other words, until the absolute value of xnxn1 is less than 0.001. You can use the built-in fabs() function from the math.h library to calculate the absolute value

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!