Cambridge O Level Computer Science 2210 · 7.9 Writing and amending algorithms

Q110 · 2210/21/M/J/25 Q4

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

Q1102210/21/M/J/25original Q4Structured · 8 marks
Question 4 from 2210/21/M/J/25

Worked solution

(a)

Plan: input the number, take 2 characters starting at position 2 (after the '+'), then compare them with the two country codes. One correct answer:

OUTPUT "Please enter a telephone number"
INPUT Telephone
Code ← SUBSTRING(Telephone, 2, 2)
IF Code = "44"
  THEN
    OUTPUT "UK"
  ELSE
    IF Code = "20"
      THEN
        OUTPUT "Egypt"
      ELSE
        OUTPUT "Unknown"
    ENDIF
ENDIF

Telephone number input correctly [1]

Use of SUBSTRING (or similar) to take the code [1]

... with the correct start and length, so the 2 digits after the '+' are extracted [1]

IF (or CASE) used to check the code, with matching ENDIF / ENDCASE [1]

Matching country output for 44 and 20, and Unknown output otherwise [1]

Other correct ways also score, for example a CASE OF statement or two separate IF statements.

(b)

Write that you need a loop that keeps asking until the length is right:

Find the length of the number entered, using LENGTH(Telephone) [1]

Use a suitable loop, for example REPEAT ... UNTIL or WHILE ... ENDWHILE, with the prompt and INPUT inside it [1]

Use the correct condition: the loop stops when LENGTH(Telephone) = 13 (a WHILE loop repeats while it is not 13) [1]

KEY IDEAUse SUBSTRING to pull out part of a string, and put a prompt inside a loop with a length test to force valid input.
TOTAL 8 marks

Official mark scheme

Mark scheme for 2210/21/M/J/25 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.