Yes I am a student

essi1553

Limp Gawd
Joined
Mar 6, 2005
Messages
303
I am having a major problem with such a simple solution:

{
if ((month == 4) && (day >= 1) && (month == 4) && (day <= 31))
penalty = (( day / 30 ) * 400);

else
if ((month == 5) && (day >= 1) && (month == 5) && (day <= 31))
penalty = ((( day / 30 ) * 400) + 400);

else
if ((month == 6) && (day >= 1) && (month == 6) && (day <= 31))
penalty = ((( day / 30 ) * 400) + 800);
}



For some odd reason I cannot have the program provide the correct calculation when I enter the correct varibles. If I satisfy any of the above arguments it calculates an answer of 800 which is the last arguement.

Am I spacing it wrong? Are the if statments contradictory (I don't see how, I think I locked them in place pretty well)? I feel like such an idiot asking this, but its driving me crazy.

I don't want the answer, per the rules, I just want a kick in the head if you will... Maybe that will makes all the pieces fall into place.

Thanks :)
 
First of all, there is quite a bit of redundancy in your code. You are checking months twice per if statement, which is unnecessary. Also, you are checking the same day bounds each time; you could pull those checks out and only check them once before you even fall into that segment of code. So if you check the day first, then all you need to do for each of those if statements is check the month.

Another problem is you are dividing an int by an int(assuming the day variable is an int), which will give you an int, which will always be 0 in this case. You'll have to get a float out of that division for the formula to work.

Not sure if that'll totally fix your problem, but it'll give you a good starting point to cleaning up the code at least.
 
Thank you for that kick in the head :)

I know I am using un-neccesacey memory buy making my lines unbelievable large and redundant, but im not to sure of myself in this field just yet. I like to try to play cautious.

I am going to re-evaluate why an int / int will give me a zero here, but that was the problem in my case. I just don't like the fact that if I had the screen print the "day value" it would come up as ie 20.00.

Thanks, lesson learned :)
 
essi1553 said:
...

I am going to re-evaluate why an int / int will give me a zero here, but that was the problem in my case.

For integer division a/b, if a >=0 and b > 0 and b > a, the result will always equal 0. With your original code, if the day were 30, the result would be correct, if 31, wrong to the same degree as when day is 1.

I just don't like the fact that if I had the screen print the "day value" it would come up as ie 20.00. ...

To fix that, change day back to an int and make the other operand of the division floating point instead.

Food for future thought: reordering operations in an equation can be used to reduce error when doing integer division (for example, (day * 400) / 30) will have less error than (day / 30) * 400)... to be exact, the error would be day/3.0) Also, reordering can be used to avoid over/underflow in the course of a calculation which should produce a result within the bounds.
 
Back
Top