Problems calculating distance and mpg

This is a discussion on Problems calculating distance and mpg within the C Programming forums, part of the General Programming Boards category; I'm supposed to build a program that calculates miles per gallon when given the radius & revolutions of a car's ...

  1. #1
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19

    Problems calculating distance and mpg

    I'm supposed to build a program that calculates miles per gallon when given the radius & revolutions of a car's tires, and the amount of gas used, but every time I try to test it out the final answer is always slightly off from the correct answer.

    Code:
    #include <stdio.h>
    
    //Constants
    const int PI = 3.14159;
    
    #define INCHES_IN_MILE 63360
    
    int main() {
    	
    	int revs;
    	double rad, gas, dist;
    	
    	//Get user input.
    	printf("What is the radius of your tires, in inches?\n");
    	scanf("%lf", &rad);
    	
    	printf("How many revolutions did your car's tires make?\n");
    	scanf("%d", &revs);
    	
    	printf("How many gallons of gas did your car use?\n");
    	scanf("%lf", &gas);
    	
    	//Calculate & print MPG
    	dist = ((rad*2*PI)*(revs)) / INCHES_IN_MILE;
    	printf("Your car averaged %.2f miles per gallon.\n", dist/gas);
    	return 0;
    }
    Did I mess up the code for the math somewhere?
    The correct math should be the radius x 2 x Pi, which is the circumference, and then that multiplied by the tire revolutions gives you the distance in inches. Divide that by the amount of inches in a mile gives you the distance traveled in miles. And when the distance is divided by the amount of gas used it should give you the mpg.

    An example of an answer I get is:

    Radius: 15
    Revolutions: 10,000
    Gallons: 0.75

    The mpg should be 19.83 but the program gives me 18.94.

    Any ideas?

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Right here.

    Code:
    const int PI = 3.14159;

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Levitylek.

    That int data type had me rolling!

  4. #4
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19
    Gah thank you so much!
    In our instructions it said something about making a Pi a constant and that tripped me up but now that I go back and read it says define a constant for Pi

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    msh gets the Eagle-eye award, methinks! Good catch!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why would you use #define for inches in a mile but not for Pi ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Program Help (MPG calculator)
    By mrwooter in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2005, 02:46 PM

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