Algorithm Design and Standard Methods: Question 7
Syllabus 7.2, 7.3, 7.4
A parcel delivery company charges a surcharge on any parcel that weighs more than 20 kg. A flowchart algorithm reads the weight of each parcel in turn, in kilograms, and counts how many parcels need the surcharge. The weights are entered one at a time; entering -1 signals that there are no more parcels to process.
The flowchart is shown below as a numbered sequence of shapes:
Step 1 [Terminator] START
Step 2 [Process] Count ← 0
Step 3 [Input/Output] INPUT Weight
Step 4 [Decision] Weight = -1 ?
Yes → go to Step 8
No → go to Step 5
Step 5 [Decision] Weight > 20 ?
Yes → go to Step 6
No → go to Step 7
Step 6 [Process] Count ← Count + 1
Step 7 [Process] go to Step 3
Step 8 [Input/Output] OUTPUT Count
Step 9 [Terminator] STOP
(a) Identify the flowchart shape used for Step 4 and Step 5, and state what type of instruction this shape represents. [2]
(b) State what this algorithm calculates, and explain how the algorithm knows when to stop reading parcel weights. [2]
(c) The company enters these parcel weights, in this order: 15, 22, 9, 30, -1. Trace the algorithm, showing the value of Weight and Count after each value is entered. [4]
(d) Rewrite this flowchart as pseudocode, using a REPEAT ... UNTIL loop. [3]
Show worked solution Hide worked solution
Worked solution
Part (a): Identifying the flowchart shape
Steps 4 and 5 (Weight = -1 ? and Weight > 20 ?) use the decision shape, drawn as a
diamond in a standard flowchart. [1] This shape represents a point where the algorithm
tests a condition and the flow of control splits into two separate paths. One followed if
the condition is true, the other if it is false, unlike a process box, which always leads to
exactly one next step. [1]
Part (b): What the algorithm calculates and when it stops
This is a counting algorithm: Count starts at 0 and only increases (Count ← Count + 1)
when a parcel’s weight tests true against Weight > 20, so after the loop ends Count holds
the number of parcels that weigh more than 20 kg and therefore need the surcharge. [1]
The algorithm does not stop after any fixed number of parcels. Instead, every time a new weight
is entered, Step 4 first checks Weight = -1 ?. As long as the answer is “No”, the flow
continues to Step 5 and eventually loops back to Step 3 to read another weight. Only when the
value -1 is entered, a sentinel value that is not a real parcel weight, does Step 4 send
the flow to Step 8 instead, ending the loop and outputting Count. [1]
Part (c): Tracing the algorithm
Starting with Count ← 0, each weight is read and tested in turn:
| Weight entered | Weight = -1 ? | Weight > 20 ? | Count after this step |
|---|---|---|---|
| 15 | No | No | 0 |
| 22 | No | Yes | 1 |
| 9 | No | No | 1 |
| 30 | No | Yes | 2 |
| -1 | Yes | (not tested) | 2 (loop ends, OUTPUT Count) |
Each correctly completed row of the trace earns credit, up to [4] for the full table,
including recognising that the sentinel value -1 ends the loop at Step 4 without ever reaching
the Weight > 20 ? test.
The algorithm outputs Count = 2, since only the parcels weighing 22 kg and 30 kg exceed 20 kg.
Part (d): Converting the flowchart to pseudocode
The flowchart’s structure, read a value, test for the sentinel, otherwise test the surcharge
condition, then loop back, maps directly onto a REPEAT ... UNTIL loop, since the input must
always happen at least once before the sentinel can be checked:
DECLARE Weight : INTEGER
DECLARE Count : INTEGER
Count ← 0
REPEAT
INPUT Weight
IF Weight <> -1 THEN
IF Weight > 20 THEN
Count ← Count + 1
ENDIF
ENDIF
UNTIL Weight = -1
OUTPUT Count
Mark-earning features: [1] Count initialised to 0 before the loop; [1] the sentinel
test IF Weight <> -1 THEN guarding the surcharge check so -1 itself is never compared against
20; [1] the loop condition UNTIL Weight = -1 correctly matching the flowchart’s Step 4
test, with OUTPUT Count placed after the loop ends.
Final answers
- (a) Decision (diamond) shape, tests a condition and branches the flow along two paths.
- (b) Counts parcels weighing more than 20 kg; stops when the sentinel value -1 is entered.
- (c) Count = 0, 1, 1, 2, then 2 is output when -1 is entered.
- (d) See the
REPEAT ... UNTILpseudocode above.