MPG Pseudocode
Gas Mileage Pseudo-code
Pseudo-code for the MPG problem:
Input startOdometer
Input finalOdometer
Input gallons
distance = finalOdometer – startOdometer
mpg = distance/gallons
Output mpg
Note the order of the statements. For the most part the statements
must be in that order.
- It is “user friendly” to ask for information in the order that it is gathered, or in the order that it is usually asked. (Imagine the errors that would occur if a form asked for the zip code first, then the state.)
- Most programs have a pattern of input, calculate, output. The program is easier to read if we input all of the information and then do all of the calculations, but sometimes a program must do some calculations before asking the user for more information.
- We had to calculate distance before we can calculate mpg because mpg uses distance.
- Obviously, you cannot output mpg until it is calculated.
We can now write the program