-
Notifications
You must be signed in to change notification settings - Fork 21
/
ex4.c
49 lines (40 loc) · 1.21 KB
/
ex4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* 4. Modify Program 7.8 so that the value of guess is printed each time
* through the while loop. Notice how quickly the value of guess converges to
* the square root. What conclusions can you reach about the number of
* iterations through the loop, the number whose square root is being
* calculated, and the value of the initial guess?
*
* By Faisal Saadatmand
*/
/*
* Answer: The greater the number whose square root is being take, the greater
* the initial guess, and thus, the more iteration through loop it would take
* to approach the value of epsilon.
*/
#include <stdio.h>
/* Function to calculate the absolute value of a number */
float absoluteValue(float x)
{
if (x < 0)
x = -x;
return x;
}
/* Function to compute the square root of a number */
float squareRoot(float x, const float epsilon)
{
float guess = 1.0;
while (absoluteValue(guess * guess - x) >= epsilon) {
guess = (x / guess + guess) / 2.0;
printf("%f\n", guess);
}
return guess;
}
int main (void)
{
const float epsilon = .00001;
printf("squareRoot(2.0) = %f\n", squareRoot(2.0, epsilon));
printf("squareRoot(144.0) = %f\n", squareRoot(144.0, epsilon));
printf("squareRoot(17.5) = %f\n", squareRoot(17.5, epsilon));
return 0;
}