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

Q68 · 2210/22/O/N/21 Q4

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

Q682210/22/O/N/21original Q4Structured · 10 marks
Question 4 from 2210/22/O/N/21

Worked solution

(a)

Check each line against what the algorithm should do. Write the line number, the fault and the fix. Write three of these faults (any three score), plus two marks for fixing the loop:

Line 8: OUTPUT Value2 should be INPUT Value2 (the second value must be read in) [1]

Line 9: IF Operator should be CASE OF Operator (it ends with ENDCASE) [1]

Line 15: OUTPUT "The answer is ", Value1 should output Answer [1]

The loop is wrong in two places. One fix: line 1 Continue ← 1 should be Continue ← 0 [1]

...and line 22 UNTIL Continue = 0 should be ENDWHILE [1]

Other loop fixes also score: change WHILE to REPEAT and use UNTIL Continue = 1; or keep UNTIL Continue = 0, change line 20 to Continue ← 0 and line 1 to Continue ← 1 with REPEAT; or WHILE Continue = 1 with line 20 Continue ← 0 and ENDWHILE.

Corrected algorithm (one version):

Continue ← 0
WHILE Continue = 0 DO
    OUTPUT "Enter 1 for +, 2 for -, 3 for * or 4 for /"
    INPUT Operator
    OUTPUT "Enter the first value"
    INPUT Value1
    OUTPUT "Enter the second value"
    INPUT Value2
    CASE OF Operator
        1: Answer ← Value1 + Value2
        2: Answer ← Value1 - Value2
        3: Answer ← Value1 * Value2
        4: Answer ← Value1 / Value2
    ENDCASE
    OUTPUT "The answer is ", Answer
    OUTPUT "Do you wish to enter more values (Yes or No)?"
    INPUT MoreValues
    IF MoreValues = "No"
        THEN
            Continue ← 1
    ENDIF
ENDWHILE

(b)

Use a loop that keeps asking for the operator while it is not 1, 2, 3 or 4.

WHILE Operator < 1 OR Operator > 4 DO
    OUTPUT "Enter 1, 2, 3 or 4"
    INPUT Operator
ENDWHILE

Appropriate loop (start and end) [1]

Tests both ends of the condition (below 1 and above 4) [1]

A suitable message is output [1]

The operator is input again until it is correct [1]

A REPEAT loop with IF ... ENDIF inside and UNTIL Operator >= 1 AND Operator <= 4 is also accepted.

Location: straight after line 4 (after INPUT Operator, before line 5) [1]

KEY IDEARead each line for what it should do; fix the faults, and validate input with a loop that repeats until the value is allowed.
TOTAL 10 marks

Official mark scheme

Mark scheme for 2210/22/O/N/21 question 4
VIDEO
▶ Topic lesson

A lesson on this topic · by Mr Bulmer's Learning Zone. 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.