1.1 ask user to enter rate per mile
2.1 calculate cost = rate x mileage 3.1 print the mileage
|
2. Code the Program The program needs to use three numbers - the number of miles, the rate per mile and the cost of the journey. We now need to decide i) what to call these numbers, an identifier and, ii) what type of numbers they are. Here's our suggestion, listed in a table:
|
identifier | type |
rate | REAL |
miles | INTEGER |
cost | REAL |
|
PROGRAM bus_journey;
{to calculate journey cost}
|
Again, you'll see that the results are not what you perhaps expected.
WRITELN('rate per mile = $', rate :5:2);
WRITELN('number of miles = ', miles); We're going to make one or two more changes now...
WRITE('enter the rate per mile '); (notice we're using WRITE instead of WRITELN)
WRITE('enter the number of miles ');
3. Test the Program Once you have the program working, you should test it to make sure it works properly. For example, we might draw up a table like the one below and check out various possibilities. What happens if you enter 6.5 for the number of miles? It is, after all, supposed to be a whole number.
|
rate | miles | expected answer | result or comment |
2 | 50 | ||
0.25 | 100 | ||
0 | 100 | ||
3 | 6.5 | ||
3 | mmm |
"The program gives correct results for whole numbers. If the user enters a decimal number for the number of miles then a 'run-time error' occurs and the program stops. This also happens if the user enters a letter instead of a number." 4. Evaluation An evaluation should say whether the program does the job that is was designed for and whether it could be improved. For example: "The program correctly calculates the cost of a journey based on the information provided by the user. Run-time errors occur if the user doesn't enter the correct type of data and it would be better if the program would allow the user to re-enter incorrect data instead of just stopping."
|
© 1998 John Brewer |