# Thread: Calculating MPG

1. ## 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. 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!  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. 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. 5. Got it. Thanks for helping me out. 6. Originally Posted by Robot Kris 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. 7. Originally Posted by Robot Kris 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. Originally Posted by Robot Kris 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 