Page 70 - Programming Microcontrollers in C
P. 70
Functions 55
{
int i;
double c;
for(i=1;i<11;i++)
{
c=sqr(i);
printf(“\t%d\t%f\t%f\n”,i,c,square(c));
}
return 0;
}
/* the square root function */
double sqr( double x )
{
double x1=1,x2=1,c;
do
{
x1=x2;
x2=(x1 + x/x1)/2;
c=x1-x2;
}
while(abs(c) >= .00000001);
return x2;
}
The result of this calculation is shown below:
1 1.000000 1.000000
2 1.414214 2.000000
3 1.732051 3.000000
4 2.000000 4.000000
5 2.236068 5.000000
6 2.449490 6.000000
7 2.645751 7.000000
8 2.828427 8.000000
9 3.000000 9.000000
10 3.162278 10.000000