Calculating MPG

This is a discussion on Calculating MPG within the C Programming forums, part of the General Programming Boards category; Hi! I am new to C programming and I just cannot figure out how to continue on my assignment. I ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Question Calculating MPG

    Hi! I am new to C programming and I just cannot figure out how to continue on my assignment. I am not asking for people to do my homework I just need a nudge in the right direction. I am trying to calculate miles per gallon from what the user inputs into the program but only tells me a number if I input large numbers and if I put in smaller numbers such as 40, 35, .8 It tells me that I averaged 00 miles per hour.
    Code:
    #include <stdio.h>
    
    #define MINUTES_IN_HOUR 60
    
    int main(void) {
    
        //Declare variables
        int minutes, average_speed, gallons, mpg; 
    
        //Prompt for user to enter data
        printf("How many minutes did you drive?\n");
        scanf("%d", &minutes);
        
        printf("What was the average speed of the car during that time?\n");
        scanf("%d", &average_speed);
        
        printf("How many gallons of gas did your car use?\n");
        scanf("%d", &gallons);
        
        //Use user data to calculate the miles per gallon
        mpg = ((minutes/MINUTES_IN_HOUR)*(average_speed))/gallons;
        printf("Your car averaged %.2d miles per gallon.\n", mpg);
        system("pause");
        
        return 0;
    }
    I am guessing it's something wrong with my calculation of miles per hour. Sorry for such a stupid question but I'm stumped.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If a car drove 100 miles, and used 1 gallon of fuel, how many miles per gallon would it have gotten?

    Time has nothing to do with this equation. Average speed has nothing to do with this equation. Just miles and gallons.

    and Welcome to the forum, Kris!
    Last edited by Adak; 09-08-2010 at 05:29 PM.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Well the thing is that I do not have the miles that were driven. I have the amount of minutes that were driven, the average speed during that time, and the amount of gallons that the car used. I must figure out how to get miles from that information. The thing is I do not know why my
    Code:
    mpg = ((minutes/MINUTES_IN_HOUR)*(average_speed))/gallons;
    is not working.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,507
    Code:
    int minutes, average_speed, gallons, mpg;
    Declare the above as float or double; unless your ARE SURE they are integer only

    Replace
    Code:
    #define MINUTES_IN_HOUR 60
    With
    Code:
    #define MINUTES_IN_HOUR (60.0)
    See Integer Division in C for the reason.

    Tim S.
    Last edited by stahta01; 09-08-2010 at 05:38 PM.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Got it. Thanks for helping me out.

  6. #6
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by Robot Kris View Post
    Got it. Thanks for helping me out.
    Your equation looked good to me. Just a subtle point though, since you're using
    the average speed, the gallons used would become average_mpg, or whatever
    notation you'd like to use.

    Edit: I have to agree with Adak, this was a pretty poor example. This assignment
    could have challenged you with programming and not using an ambiguous variables
    like average speed. Maybe if you were given, X gallons when starting the trip, after
    Y mins the car ran out of gas... calculate the mpg.
    Last edited by Char*Pntr; 09-08-2010 at 06:10 PM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Robot Kris View Post
    Well the thing is that I do not have the miles that were driven. I have the amount of minutes that were driven, the average speed during that time, and the amount of gallons that the car used. I must figure out how to get miles from that information. The thing is I do not know why my
    Code:
    mpg = ((minutes/MINUTES_IN_HOUR)*(average_speed))/gallons;
    is not working.
    Kris... take a close look at your final printout statements. Recheck the formatting string and the variable you are printing.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Robot Kris View Post
    Well the thing is that I do not have the miles that were driven. I have the amount of minutes that were driven, the average speed during that time, and the amount of gallons that the car used. I must figure out how to get miles from that information. The thing is I do not know why my
    Code:
    mpg = ((minutes/MINUTES_IN_HOUR)*(average_speed))/gallons;
    is not working.

    Your answer has been mentioned, but let's talk about the process of solving a problem like this one.

    You don't have the distance, and that's what you need. Can you calculate the distance?

    Think about distance = rate * time. Where rate is the average speed.

    When you have a problem with a final calculation, check your intermediate results that lead up to it. In this case, by checking that you have the distance, and that the number of miles (kilometers) is what it should be. Is the distance your program giving you, a reasonable one? Can you confirm it by giving it a few test cases?

    THEN look at your final equation, and your final answer.

    Narrowing down the problem in a program is a very important skill for all programmers. Checking your intermediate results, really helps speed that process up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating : high numbers
    By MiraX33 in forum C++ Programming
    Replies: 9
    Last Post: 06-08-2006, 11:08 PM
  2. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 09:38 AM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. Taking input while calculating
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 04:47 PM
  5. Calculating window sizes
    By Mox in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2001, 08:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21