Home › Forums › C Programming › calculate and display mpg in C program
- This topic has 5 replies, 3 voices, and was last updated 15 years, 11 months ago by GWILouisaxwzkla.
- AuthorPosts
- December 14, 2008 at 5:27 pm #2167SilviaJamiesonParticipant
Hi, I wonder if anyone can help me with the following problem. If so, thank you, if not, no problem…
Because of the high price of gasoline, drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tanks of gasoline by recording miles driven and gallons used for each tank.
– Develop a C program that will input the miles driven and gallons used for each tank.
– The program should calculate and display the miles per gallon obtained for each tank.
– After processing all input information, the program should calculate and print the
combined miles per gallon obtained for all tank.
– The program should terminate when the user enters gallons used of -1An example of input/output dialog is shown below
Enter the gallons used (-1 to end) : 12.8
Enter the miles driven : 287
The miles/gallon for this tank was : 22.421875
Enter the gallons used (-1 to end) : 10.3
Enter the miles driven : 200
The miles/gallon for this tank was : 19.417475
Enter the gallons used (-1 to end) : 5
Enter the miles driven : 120
The miles/gallon for this tank was : 24.000000
Enter the gallons used (-1 to end) : -1
The overall average miles/gallon was 21.601423 - December 15, 2008 at 7:59 pm #3493GWILouisaxwzklaParticipant
Could do:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : December,15,2008<br />* Comments : new project<br />* Compiler/Assembler :<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 2:39:00 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include < stdio.h ><br />//#include < string.h ><br />//#include < conio.h ><br />//#include < math.h ><br />//#include < iomanip ><br />//#include < ctype.h ><br /><br />//using namespace std;<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />float gallons , miles;<br /><br />printf ( "Enter the gallons used (-1 to end) : " );<br />scanf ( "%f" , & gallons );<br />while ( gallons != -1 )<br />{<br />printf ( "Enter the miles driven : " );<br />scanf ( "%f" , & miles );<br />printf ( "The miles/gallon for this tank was : %f n" , miles / gallons );<br />printf ( "Enter the gallons used (-1 to end) : " );<br />scanf ( "%f" , & gallons );<br />}<br />return 0 ;<br />}<br /><br /><br /><br /> - December 16, 2008 at 7:01 am #3494AdetutuParticipant
HI Dman,
Please confirm me. How will this code calculate Overall Average MIles/Gallon used which is last statement of program?I fear that it is missing.
Thanks
Gaurav - December 21, 2008 at 10:57 pm #3495GWILouisaxwzklaParticipant
The average is calculated in the line:
123<br />printf ( "The miles/gallon for this tank was : %f n" , miles / gallons );<br />the compiler sets up a hidden temporary variable to hold the result of ( miles/gallons ) and then sends this result to the printf () function to print it……..
- January 22, 2009 at 3:25 pm #3496AdetutuParticipant
Hello dman,
If I am understanding your program correctly, It will not calculate overall average of miles/gallon. If you want to calculate average, you need to keep track number of inputs, and perform addition of each calculation of miles/gallon and then divide sum from number of input.for eg For given 3 input 22.421875 ,19.417475 ,24.000000
overall average = (22.421875 + 19.417475 + 24.000000)/3
I didn’t run this program on any compiler but somehow i am not able to see this average calculation in the program.
Please feel free to revert back.
Gaurav - January 24, 2009 at 8:31 pm #3497GWILouisaxwzklaParticipant
I think the calculation you are making here:
123<br />overall average = (22.421875 + 19.417475 + 24.000000)/3<br />is the average of the average fuel milages. I belive the calculation for average fuel milage is in fact ( miles ) / ( gallons ) and that this quantity is named “average” since calculating “actual fuel milage” would require introducing a set of standard conditions per vehicle ( standard temperature , air pressure , fuel mixture , road surface friction , weather conditions , driving style , ect. ) to arrive at the value of actual miles per gallon. Since simply dividing the number of miles driven by the number of gallons consumed ignores all the possible parameters in the problem the calculation is refered to as “average miles per gallon”. I’ll look into this but thats my guess…
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.