anybody wanna take a look and see if this can be streamlined anymore. the mpg needs to be cumulative, can that be done w/out an if/else?
Code:if (i=0) { mpg=miles/gallons; } else { mpg += (miles/gallons)/2; } i++;
anybody wanna take a look and see if this can be streamlined anymore. the mpg needs to be cumulative, can that be done w/out an if/else?
Code:if (i=0) { mpg=miles/gallons; } else { mpg += (miles/gallons)/2; } i++;
I think you want if (i==0) instead of if(i=0)
Originally posted by blight2c
anybody wanna take a look and see if this can be streamlined anymore. the mpg needs to be cumulative, can that be done w/out an if/else?
Code:if (i=0) { mpg=miles/gallons; } else { mpg += (miles/gallons)/2; } i++;orCode:mpg += (miles/gallons)/2; if (i == 0) mpg = miles/gallons; i++
Code:i == 0 ? mpg = miles/gallons : mpg += (miles/gallons)/2; i++;
Last edited by salvelinus; 04-02-2002 at 09:54 AM.
Also, in the above statement, mpg will never be cumulative; anytime the assignment in this if statement executes, mpg will be set to the calculated value; the calculated value won't be added to mpg.Originally posted by blight2c
anybody wanna take a look and see if this can be streamlined anymore. the mpg needs to be cumulative, can that be done w/out an if/else?
Code:if (i=0) { mpg=miles/gallons; }
The else part, however you code it, will be cumulative.
right, i should have been more clear. the if is only for the first time around when there have been no previos mpg's to calc. the second time around (all this is in a while loop, btw) and thereafter it should always flow into the else. i just found it cumbersome and inefficent. i was wondering if there was a more clever algorithm. thanks for the helpAlso, in the above statement, mpg will never be cumulative; anytime the assignment in this if statement executes, mpg will be set to the calculated value; the calculated value won't be added to mpg.
In that case, do the first calculation before you enter the loop. Subsequent calculations can be done in the loop. You shouldn't need to check i then, won't need an if.
Based on the code you posted, maybe other stuff in your program would still require it.