Skip to content

Commit 5e5d6cf

Browse files
committed
function and scope
1 parent e41f105 commit 5e5d6cf

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

prog.8.8.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Function to calculate the absolute value of a number
2+
3+
#include <stdio.h>
4+
5+
float absoluteValue (float x)
6+
{
7+
if ( x < 0 )
8+
x = -x;
9+
return (x);
10+
}
11+
12+
// Function to compute the square root of a number
13+
14+
float squareRoot (float x)
15+
{
16+
const float epsilon = .00001;
17+
float guess = 1.0;
18+
19+
while ( absoluteValue (guess * guess - x) >= epsilon )
20+
guess = ( x / guess + guess ) / 2.0;
21+
22+
return guess;
23+
}
24+
25+
int main (void)
26+
{
27+
printf ("squareRoot (2.0) = %f\n", squareRoot (2.0));
28+
printf ("squareRoot (144.0) = %f\n", squareRoot (144.0));
29+
printf ("squareRoot (17.5) = %f\n", squareRoot (17.5));
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)