The numbers I need to input test are as following:
Start milepost? 321
Elapsed time (hours minutes seconds)? 2 15 36
End milepost? 458
The result should be that the car traveled 137 miles in 2 hrs 15 min and 36 sec.
That is correct.
The only thing wrong is that the average velocity SHOULD be 60.6195 mph.
I am receiving a value of 68.5.
Here is my code, please help, and explain if you would be so kind.
hourstotal=(hours)+(minutes/60)+(seconds/3600);
hours, minutes and seconds are all integers. So, minutes / 60 and seconds / 3600 result in integer division, and the result of that division is 0. Try casting minutes and seconds to floats:
Edit:
Or if you chose to change to double or long double, cast them to that. Damn ninjas are breaking the internet. Had to reload nearly a dozen times before the page would actually come up.
I think you're running into an integer division problem (whereby the remainder is thrown away). You are getting minutes/60 = 0 and seconds/3600 = 0 giving hourstotal = 2 exactly (which fits your result).
Force floating point division by making one number a float value. Try replacing line 24 with hourstotal=(hours)+(minutes/60.0f)+(seconds/3600.0f);
This worked, thank you so much everybody! I guess dividing integers is not something that should be done for precision.
I did as Danny Toledo said and casted the minutes and seconds to floats and that seemed to fix all of my issues.