Course 8.2Lesson 3 of 3
A program is read far more often than it is written. This lesson shows how to make pseudocode maintainable, with meaningful identifiers, comments, constants and subroutines, and how to plan and write the long scenario answer at the end of Paper 2.
Try the code and the simulations beside the lesson.
Open an old school notebook and you may not understand your own notes. Programs are the same. A maintainable program is one that another person, or you a year later, can read, correct and change. The syllabus asks for four habits.
AdultFare, not a.// The cost of one day of trips.Here is a working program. Can you say what it does?
DECLARE A : INTEGER
DECLARE B : INTEGER
DECLARE C : REAL
INPUT A
INPUT B
C ← A * 2.40 + B * 1.20
IF C > 6.00
THEN
C ← 6.00
ENDIF
OUTPUT CIt works out a day of bus fares with a daily cap, but nothing tells you so. Here is the same algorithm, written to be maintained:
// Bus fares for one day, with a daily cap
CONSTANT AdultFare ← 2.40
CONSTANT ChildFare ← 1.20
CONSTANT DayCap ← 6.00
// The cost of one day of trips, never more than the cap
FUNCTION DayCost(AdultTrips : INTEGER, ChildTrips : INTEGER) RETURNS REAL
DECLARE Cost : REAL
Cost ← AdultTrips * AdultFare + ChildTrips * ChildFare
IF Cost > DayCap
THEN
Cost ← DayCap
ENDIF
RETURN ROUND(Cost, 2)
ENDFUNCTION
DECLARE Adults : INTEGER
DECLARE Children : INTEGER
OUTPUT "Adult trips today?"
INPUT Adults
OUTPUT "Child trips today?"
INPUT Children
OUTPUT "Cost today: ", DayCost(Adults, Children)For 1 adult trip and 2 child trips the output is Cost today: 4.8, because . For 3 adult trips and 1 child trip the cost would be , so the cap gives Cost today: 6.0. When the fares change next year, only the three constants change. The function can be tested on its own with any two numbers. Single letters are fine only where they are a convention, such as i and j for array positions.
The last question of Paper 2 gives a scenario and asks for a whole program. It is worth 15 marks. You may answer in pseudocode, Python, Visual Basic or Java. The marks are for the logic, not for the exact spelling of a language. A plan helps.
Each task is usually worth a mark, and so are good layout and comments. A program that does most of the tasks scores well, even if one part is missing. So never leave the answer blank.
The program is the tidy bus-fare program in Python, tested with three days.
x, temp2 or data. Say what the value is.6.00 typed in several places. Use a constant.// add 1 to Count. Say why, or what a part does.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
n that holds the number of students in a class.CONSTANT VatRate ← 0.2 better than typing 0.2 in five places?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place in the course.