< previous page page_82 next page >

Page 82
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
NameValueDescription
AMT111.7Number of gallons for fillup 1
AMT214.3Number of gallons for fillup 2
AMT312.2Number of gallons for fillup 3
AMT48.5Number of gallons for fillup 4
START_MILES67308.0Starting mileage
END_MILES68750.5Ending mileage

Variables
NameData TypeDescription
mpgfloatComputed miles per gallon

 
< previous page page_82 next page >