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

Q113 · 2210/22/O/N/25 Q4

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

Q1132210/22/O/N/25original Q4Structured · 7 marks
Question 4 from 2210/22/O/N/25

Worked solution

(a)

Write: to check that the data entered has the required number of characters (the right length). [1]

(b)

Start at the first character. Look at one character at a time with SUBSTRING. Stop when an @ is found or the end is reached. Then output a message.

INPUT EmailAddress
Start ← 1
Valid ← FALSE
REPEAT
    IF SUBSTRING(EmailAddress, Start, 1) = '@'
      THEN
        Valid ← TRUE
      ELSE
        Start ← Start + 1
    ENDIF
UNTIL Start > LENGTH(EmailAddress) OR Valid = TRUE
IF Valid = TRUE
  THEN
    OUTPUT EmailAddress, " is valid"
  ELSE
    OUTPUT EmailAddress, " is NOT valid"
ENDIF

Start position and the flag set before the loop (Start ← 1, Valid ← FALSE) [1]

Email address input into a variable [1]

A condition-controlled loop (REPEAT or WHILE) that stops when @ is found or no characters are left [1]

SUBSTRING used correctly to pick out one character, which is compared with '@' [1]

If '@' is found the loop ends (flag set to TRUE); if not, move to the next character [1]

Output that shows a message for valid and a message for not valid (both cases) [1]

Other correct loops, flags or ways of stopping the loop also score.

KEY IDEACheck each character in turn with SUBSTRING in a condition-controlled loop, stop when you find the symbol, then output a message for both outcomes.
TOTAL 7 marks

Official mark scheme

Mark scheme for 2210/22/O/N/25 question 4
VIDEO
▶ Whole paper

Video solution of the whole paper · by MAK CS. 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.