Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. //This program will determine the MPG of a vehicle given the miles driven and the gallons of gas list
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6.  
  7. void main(void)
  8. {
  9. // Defining the variables for the program
  10. float gallons, mpg;
  11. int miles,iteration;
  12. float mpgt, mpga;
  13.  
  14. //initializing the float values
  15.  
  16. gallons = 0;
  17. mpg = 0;
  18. mpgt = 0;
  19. mpga = 0;
  20. iteration = 0;
  21.  
  22. //while loop that will run program through multiple iterations
  23. while (gallons != -1)
  24. {
  25. //Obtaining the number of gallons used
  26. printf("Please enter gallons used(enter -1 to end):\n");
  27. scanf_s("%f", &gallons);
  28. //Obtaining the
  29. printf("Please enter miles driven:\n");
  30. scanf_s("%d", &miles);
  31.  
  32. //Calculating Miles per Gallon
  33.  
  34. mpg = (float)miles / gallons;
  35.  
  36. printf("The Miles per Gallon for this tank was %.5f\n", mpg);
  37. //Stores the total mpg over x iterations
  38. mpgt = mpgt + mpg;//This was the line in question. this was my solution
  39. //keeps track of the number of iterations performed
  40. iteration = ++iteration;
  41. }
  42. mpga = mpgt / iteration;
  43.  
  44. printf("mpgt = %f", mpgt);
  45.  
  46. printf("The average MPG of all tanks is %.6f\n", mpga);
  47.  
  48. printf("\n\n");
  49. system("pause");
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement