Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This program will determine the MPG of a vehicle given the miles driven and the gallons of gas list
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- void main(void)
- {
- // Defining the variables for the program
- float gallons, mpg;
- int miles,iteration;
- float mpgt, mpga;
- //initializing the float values
- gallons = 0;
- mpg = 0;
- mpgt = 0;
- mpga = 0;
- iteration = 0;
- //while loop that will run program through multiple iterations
- while (gallons != -1)
- {
- //Obtaining the number of gallons used
- printf("Please enter gallons used(enter -1 to end):\n");
- scanf_s("%f", &gallons);
- //Obtaining the
- printf("Please enter miles driven:\n");
- scanf_s("%d", &miles);
- //Calculating Miles per Gallon
- mpg = (float)miles / gallons;
- printf("The Miles per Gallon for this tank was %.5f\n", mpg);
- //Stores the total mpg over x iterations
- mpgt = mpgt + mpg;//This was the line in question. this was my solution
- //keeps track of the number of iterations performed
- iteration = ++iteration;
- }
- mpga = mpgt / iteration;
- printf("mpgt = %f", mpgt);
- printf("The average MPG of all tanks is %.6f\n", mpga);
- printf("\n\n");
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement