Home Forums C Programming calculate and display mpg in C program

Viewing 5 reply threads
  • Author
    Posts
    • #2167
      SilviaJamieson
      Participant

      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 -1

      An 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

    • #3493
      GWILouisaxwzkla
      Participant

      Could do:

    • #3494
      Adetutu
      Participant

      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

    • #3495
      GWILouisaxwzkla
      Participant

      The average is calculated in the line:

      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……..

    • #3496
      Adetutu
      Participant

      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

    • #3497
      GWILouisaxwzkla
      Participant

      I think the calculation you are making here:

      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…

Viewing 5 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.