Worked solution from the Algorithm design and problem-solving Paper 2 Algorithms, Programming and Logic booklet, Part 8 of 8.
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.
| Password | PasswordRepeat | OUTPUT |
|---|---|---|
| Please enter password | ||
| Secret | Reject | |
| Please enter password | ||
| Secret | Reject | |
| Please enter password | ||
| VerySecret | VerySecret | Accept |
| Please enter password | ||
| Pa55word | Pa55word | Accept |
| Please enter password | ||
| 999 | Reject |
Password column correct [1]
PasswordRepeat column correct [1]
OUTPUT column correct [1]
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.
▶ 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.