Friday, July 31, 2009

Int a[20];?

int a[20];


int b[20]; //Assume a and b have been initialized by


float c[20]; //non-zero values





for(int i=0;i%26lt;10;i++)


{


c[i]=(b[i+1]-b[i])/(a[i+1...


}





now if i get %26lt;1 solutions for the above operation then c gets stored with 0 for that value. but it is float so it should store 0.4 or 0.5 etc....





using c[i]=float((b[i+1]-b[i])/... dosen't work


but if a and b are declared as float, then the problem is solved and i get decimal values less than one stored in it.





Can anyone explain why?

Int a[20];?
The reason is this, a/b is an integer result if a and b are integers.





Your proposed cast is (float) (a/b) - in this case, you still have the integer division, you have not changed anything.





Try this instead: (float) a / b. In this case a is cast to be a float BEFORE the division, and dividing a float by an integer will be a float division.





Hope this helps.


No comments:

Post a Comment