Hi!
Please take a look at the code and tell me, why there is an infinite loop in recursive function?
Code:
# include <stdio.h>
void pIt (float X1);
void pRe (float X1, int I);
int main ()
{
float X1 = 100;
int I = 100;
printf ("Iterative function\n");
pIt (X1);
printf ("\n\nRecursive function\n");
pRe (X1, I);
getchar ();
return 0;
}
void pIt (float X1)
{
int I = 100;
while ( (I > 0) && (X1 > 1) )
{
X1 = (float) (X1 * 0.5);
I = I - 1;
}
printf ("X1 = %f", X1);
}
void pRe (float X1, int I)
{
while ( (I > 0) && (X1 > 1) )
{
pRe ((float) (X1*0.5), I-1);
printf ("\n1. X1 = %f", X1); // X1 here is not under 1 - WHY ??
printf ("\nI = %d", I);
getchar ();
}
printf ("2. X1 = %f", X1); // X1 is here under 1
}
Thanks.