Validation, Verification and Testing: Question 10
Syllabus 7.7, 7.8, 7.9
A weather station stores five overnight temperature readings (in °C) in an array. The pseudocode below is meant to find and output the highest of the five readings.
01 DECLARE Temp : ARRAY[1:5] OF INTEGER
02 DECLARE Highest : INTEGER
03 DECLARE i : INTEGER
04 Temp[1] ← -6
05 Temp[2] ← -2
06 Temp[3] ← -9
07 Temp[4] ← -1
08 Temp[5] ← -4
09 Highest ← 0
10 FOR i ← 1 TO 5
11 IF Temp[i] > Highest
12 THEN
13 Highest ← Temp[i]
14 ENDIF
15 NEXT i
16 OUTPUT Highest
(a) Complete a trace table showing the values of i, Temp[i] and Highest as the algorithm
executes, and state the value output. [4]
(b) The value that should be output is −1, since −1 is the highest of the five readings. Explain why the algorithm instead produces the value found in part (a), identifying the line responsible. [2]
(c) Rewrite lines 09 and 10 of the pseudocode so that the algorithm correctly finds the highest reading, even when every reading is negative. [2]
Show worked solution Hide worked solution
Worked solution
Part (a): Tracing the FOR loop
Highest starts at 0 (line 09), and the loop on lines 10-15 checks each Temp[i] in turn against
Highest:
| i | Temp[i] | Highest |
|---|---|---|
| 1 | −6 | 0 |
| 2 | −2 | 0 |
| 3 | −9 | 0 |
| 4 | −1 | 0 |
| 5 | −4 | 0 |
At every step, Temp[i] is negative, so the condition on line 11, Temp[i] > Highest (that is,
Temp[i] > 0), is false. Line 13 never runs, and Highest stays at 0 for all five iterations.
Line 16 then outputs Highest, which is 0. [4 marks]: [1] for a correct Temp[i]
column, [1] for correctly showing Highest never changes from 0, [1] for correctly
completing all five rows of the trace, [1] for the correct output, 0.
Part (b): Why the output is wrong
The five readings are −6, −2, −9, −1 and −4, so the true highest reading is −1. The algorithm
instead outputs 0 because line 09 initialises Highest to 0, a value that is greater than
every one of the five (negative) readings. As a result, the condition on line 11 (Temp[i] > Highest) is never satisfied for any reading, so Highest is never updated away from its starting
value of 0. The fault is the initial value chosen on line 09, not the comparison or loop structure
itself. [2 marks]: [1] for identifying line 09’s initialisation of Highest to 0 as the
cause, [1] for explaining that this makes the condition on line 11 impossible to satisfy when
all readings are negative.
Part (c): Correcting the pseudocode
Highest must start as an actual reading from the data, not an arbitrary fixed value, so it
should be initialised using Temp[1], and the loop only needs to check the remaining readings,
Temp[2] to Temp[5]:
09 Highest ← Temp[1]
10 FOR i ← 2 TO 5
Tracing this corrected version confirms it works: Highest starts at Temp[1] = −6. At i=2,
Temp[2] = −2 > −6, so Highest ← −2. At i=3, Temp[3] = −9 > −2 is false, so Highest stays
−2. At i=4, Temp[4] = −1 > −2, so Highest ← −1. At i=5, Temp[5] = −4 > −1 is false, so
Highest stays −1. The final output is −1, the correct highest reading. [2 marks]:
[1] for initialising Highest to Temp[1] on line 09, [1] for correctly adjusting the
FOR loop to start at i ← 2 on line 10.
Final answers
- (a)
Higheststays 0 throughout; output = 0 (incorrect). - (b) Line 09 initialises
Highestto 0, which is greater than every negative reading, so the condition on line 11 is never true andHighestis never updated. - (c)
09 Highest ← Temp[1]and10 FOR i ← 2 TO 5; the corrected algorithm outputs −1.