Thanks. I forgot thatOriginally posted by Hammer
The next size up variable from an int is a "long int". A float holds numbers with digits after the decimal point.
Thanks. I forgot thatOriginally posted by Hammer
The next size up variable from an int is a "long int". A float holds numbers with digits after the decimal point.
Although the problem seems solved, lemme go ahead and point out why the above code will not work...This is a nested loop, the outside loop runs through each power of the polynomial, the inner loop handles calculating the power. BUT in order for the inner loop to work, the value of result must be 1 at the beginning of the loop (and it is easy to show that this is not so for any except the first iteration of the outer loop.Code:int EVALUATE (int poly[],int num) { int i,m,temp,total=0,result=1; for(i=9;i>=0;i--) { temp=poly[i]; for(m=0;m<i;m++) result*=num; total+=(result*temp); } printf ("%d\n", total); return total; }That should work, provided the result is within the bounds of an int.Code:int EVALUATE (int poly[],int num) { int i,m,temp,total=0,result=1; for(i=9;i>=0;i--) { temp=poly[i]; result = 1; // <- The change for(m=0;m<i;m++) result*=num; total+=(result*temp); } printf ("%d\n", total); return total; }
Callou collei we'll code the way
Of prime numbers and pings!
Originally posted by QuestionC
Although the problem seems solved, lemme go ahead and point out why the above code will not work...This is a nested loop, the outside loop runs through each power of the polynomial, the inner loop handles calculating the power. BUT in order for the inner loop to work, the value of result must be 1 at the beginning of the loop (and it is easy to show that this is not so for any except the first iteration of the outer loop.Code:int EVALUATE (int poly[],int num) { int i,m,temp,total=0,result=1; for(i=9;i>=0;i--) { temp=poly[i]; for(m=0;m<i;m++) result*=num; total+=(result*temp); } printf ("%d\n", total); return total; }That should work, provided the result is within the bounds of an int.Code:int EVALUATE (int poly[],int num) { int i,m,temp,total=0,result=1; for(i=9;i>=0;i--) { temp=poly[i]; result = 1; // <- The change for(m=0;m<i;m++) result*=num; total+=(result*temp); } printf ("%d\n", total); return total; }
You're right.
I was so happy when it worked twice I just stopped. should have tried a few more times
Thanks.