|
|
|
|
|
|
|
Discussion: If you calculated this by hand, you would add up the gallon amounts, then divide the sum into the mileage traveled. The mileage traveled is, of course, just the ending mileage minus the starting mileage. This is essentially the algorithm we use in the program. Let's make all of the numeric quantities named constants, so that it is easier to change the program later. Here is the algorithmic solution: |
|
|
|
|
|
|
|
|
AMT1 = 11.7
AMT2 = 14.3
AMT3 = 12.2
AMT4 = 8.5
START_MILES = 67308.0
END_MILES = 68750.5
Set mpg = (END_MILES - START_MILES) / (AMT1 + AMT2 + AMT3 + AMT4)
Write the fillup amounts
Write the starting mileage
Write the ending mileage
Write the mileage per gallon |
|
|
|
|
|
|
|
|
From the algorithm we can create tables of constants and variables that help us write the declarations in the program. |
|
|
|
|
| Constants | | Name | Value | Description | | AMT1 | 11.7 | Number of gallons for fillup 1 | | AMT2 | 14.3 | Number of gallons for fillup 2 | | AMT3 | 12.2 | Number of gallons for fillup 3 | | AMT4 | 8.5 | Number of gallons for fillup 4 | | START_MILES | 67308.0 | Starting mileage | | END_MILES | 68750.5 | Ending mileage |
|
|
| Variables | | Name | Data Type | Description | | mpg | float | Computed miles per gallon |
|
|
|