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

Q90 · 2210/22/M/J/21 Q4

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

Q902210/22/M/J/21original Q4Structured · 7 marks
Question 4 from 2210/22/M/J/21

Worked solution

(a)

Run the algorithm one input at a time. A password shorter than 8 characters is rejected at once. A password of 8 or more characters is followed by a second input, which is compared with the first.

Secret has 6 characters, so it is rejected without asking for a repeat. VerySecret and Pa55word each have 8 or more characters and match their repeat, so they are accepted. 999 has fewer than 8 characters, so Reject is output, then the loop stops.

PasswordPasswordRepeatOUTPUT
Please enter password
SecretReject
Please enter password
SecretReject
Please enter password
VerySecretVerySecretAccept
Please enter password
Pa55wordPa55wordAccept
Please enter password
999Reject

Password column correct [1]

PasswordRepeat column correct [1]

OUTPUT column correct [1]

(b)

Add a counter for the number of tries and put a loop around the part that asks for the repeat password.

Add a counter (or a flag) before the first INPUT, and set it to 0 at the start [1]

After IF Length(Password) >= 8 THEN (or after INPUT PasswordRepeat), start a REPEAT or WHILE loop so the password can be checked again [1]

After OUTPUT "Reject", add 1 to the counter, output a message such as "Try again" and add INPUT PasswordRepeat again [1]

After the ENDIF, end the loop with UNTIL (or ENDWHILE) so it stops after three tries or when the repeated password matches the first one [1]

Also accepted: after OUTPUT "Accept", reset the flag to show the passwords matched.

KEY IDEACount the tries with a counter and wrap a loop round the check so it repeats until it matches or the counter reaches 3.
TOTAL 7 marks

Official mark scheme

Mark scheme for 2210/22/M/J/21 question 4
VIDEO
▶ Whole paper

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