求助
```// 此程序求絕對值
float absolutevalue (float x)
{
if ( x < 0)
x = -x;
return x;
}
//此程序求squreRoot 中 while 循環次數
int times (float esplion){
int i;
while (esplion != 1) {
esplion *= 10;
i ++;
}
return i;
}
//此程序求平方根
float squreRoot (float x, float esplion)
{
float guess = 1.0;
while (absolutevalue (guess*guess - x) >= esplion )
{
int i;
for ( i = 0; i<= times(esplion); i++)
{
guess = (x/guess + guess)/2.0;
printf ("The guess is %f.\n", guess);
}
}
return guess;
}
int main ()
{
printf ("Enter you number : \n");
float x, esplion ;
scanf ("%f", &x);
printf ("Enter you esplion: \n");
scanf ("%f", &esplion);
printf ("The squreroot of the %f is %f", x, squreRoot (x, esplion));
return 0;
}
squreRoot 效率很低,times(esplion)只需要調用一次
esplion取值不當的話,times方法的實現會導致死循環