Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a C++ program that uses a while statement to input the miles driven and gal- lons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful and
print the combined miles per gallon obtained for all tankfuls up to this point.c
Enter miles driven (-1 to quit): 287
Enter gallons used: 13
MPG this tankful: 22.076923
Total MPG: 22.076923
Enter miles driven (-1 to quit): 200
Enter gallons used: 10
MPG this tankful: 20.000000
Total MPG: 21.173913
Enter the miles driven (-1 to quit): 120
Enter gallons used: 5
MPG this tankful: 24.000000
Total MPG: 21.678571
Enter the miles used (-1 to quit): -1
Top:
Determine the current and combined miles/gallon for each tank of gas
First refinement:
Initialize variables
For each tank of gas, input the miles driven and gallons used, then calculate and print
the miles/gallon for that tank of gas
Calculate and print the overall average miles/gallon
Second refinement:
Initialize totalGallons to zero
Initialize totalMiles to zero
Prompt the user to enter the miles used for the first tank
Input the miles used for the first tank (possibly the sentinel)
While the sentinel value (-1) has not been entered for the miles
Prompt the user to enter the gallons used for the current tank
Input the gallons used for the current tank
Add miles to the running total in totalMiles
Add gallons to the running total in totalGallons
If gallons is not zero
Calculate and print the miles/gallon
If totalGallons is not zero
Calculate and print the totalMiles/totalGallons
Prompt the user for the next tank’s number of miles
Input the miles used for the next tank
```
// Calculate average MPG with sentinel-controlled repetition.
#include
using namespace std;
int main()
{
double gallons; // gallons used for current tank
double miles; // miles driven for current tank
double totalGallons = 0; // total gallons used
double totalMiles = 0; // total miles driven
double milesPerGallon; // miles per gallon for tankful
double totalMilesPerGallon; // miles per gallon for trip
// processing phase
// get miles used for first tank
cout << "Enter miles driven (-1 to quit): ";
cin >> miles;
cout << fixed; // set floating-point number format
// exit if the input is -1; otherwise, proceed with the program
while ( miles != -1 )
{
// prompt user for gallons and obtain the input from user
cout << "Enter gallons used: ";
cin >> gallons;
// add gallons and miles for this tank to total
totalMiles += miles;
totalGallons += gallons;
// calculate miles per gallon for the current tank
if ( gallons != 0 )
{
milesPerGallon = miles / gallons;
cout << "MPG this tankful: " << milesPerGallon;
} // end if
// calculate miles per gallon for the total trip
if ( totalGallons != 0 )
{
totalMilesPerGallon = totalMiles / totalGallons;
cout << "\nTotal MPG: " << totalMilesPerGallon;
} // end if
// prompt user for new value for miles
cout << "\n\nEnter miles driven (-1 to quit): ";
cin >> miles;
} // end while
} // end main
```
Enter miles driven (-1 to quit): 287
Enter gallons used: 13
MPG this tankful: 22.076923
Total MPG: 22.076923
Enter miles driven (-1 to quit): 200
Enter gallons used: 10
MPG this tankful: 20.000000
Total MPG: 21.173913
Enter miles driven (-1 to quit): 120
Enter gallons used: 5
MPG this tankful: 24.000000
Total MPG: 21.678571
Enter miles driven (-1 to quit): -1
You might also like to view...
The activities that ensure the overall security of the system during routine operation are known as ____.
A. monitoring duties B. evaluation duties C. functional duties D. operational duties
Which of the following options should be used if you are planning not to use your computer for a short time and want it to reactivate quickly?
A. Log off B. Hibernate C. Switch user D. Sleep
Oracle Standard supports ____ CPU sockets.
A. 1 B. 2 C. 3 D. 4
A(n) ________ device facilitates file sharing and data backup.
A. NAS B. WAN C. UTP D. NOS