Cambridge O Level Computer Science 2210 · 7.8 Identifying and correcting errors in algorithms

Q70 · 2210/21/M/J/23 Q5

Worked solution from the Algorithm design and problem-solving Paper 2 Algorithms, Programming and Logic booklet, Part 7 of 8.

Q702210/21/M/J/23original Q5Structured · 8 marks
Question 5 from 2210/21/M/J/23

Worked solution

(a)

Check the algorithm line by line against what it should do: total the positive numbers, stop on 0, output the total.

Error 1: line 04 IF Number < 0. Correction: write IF Number > 0 so positive numbers are totalled. [1]

Error 2: line 10 Exit ← 1 (Exit is already 1, so the loop never stops). Correction: write Exit ← 0 (or set Exit to 0 at line 01 and make line 02 WHILE Exit = 0). [1]

Error 3: line 13 ENDIF is an extra ENDIF that closes nothing. Correction: write ENDWHILE to close the loop. [1]

Error 4: line 14 outputs Number, not the total. Correction: write OUTPUT "The total value of your numbers is ", Total. [1]

01 Exit ← 1
02 WHILE Exit <> 0 DO
03     INPUT Number
04     IF Number > 0
05         THEN
06             Total ← Total + Number
07         ELSE
08             IF Number = 0
09                 THEN
10                     Exit ← 0
11             ENDIF
12     ENDIF
13 ENDWHILE
14 OUTPUT "The total value of your numbers is ", Total

(b)

Add a counter that goes up only when a positive number is added to the total.

Initialise a new counting variable before the loop, for example Count ← 0 [1]

Insert a counting statement between lines 05 and 07, so only positive numbers are counted [1]

... for example Count ← Count + 1 [1]

Add a new output after the loop (after line 13), for example OUTPUT Count [1]

KEY IDEATrace each line to find errors, and count only inside the branch that handles the items you want to count.
TOTAL 8 marks

Official mark scheme

Mark scheme for 2210/21/M/J/23 question 5
VIDEO
▶ Whole paper

Video solution of the whole paper · by ZAINEMATICS. Tap the picture to open it on YouTube.

Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.