9618/23/M/J/22 question 9: worked solution (May/June 2022, paper 23)
Show as plain textHide plain text
Cambridge AS Level Computer Science 9618 · 10.2 Arrays Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4. Q18 9618/23/M/J/22 original Q9 Structured · 16 marks Worked solution (a) Build the password from three groups. In each group, keep picking random characters until you get one that is not already in that group. FUNCTION Generate() RETURNS STRING DECLARE Password, Group : STRING DECLARE NextChar : CHAR DECLARE GroupCount, CharCount : INTEGER Password ← "" FOR GroupCount ← 1 TO 3 Group ← "" FOR CharCount ← 1 TO 4 REPEAT NextChar ← RandomChar() UNTIL Exists(Group, NextChar) = FALSE Group ← Group & NextChar NEXT CharCount Password ← Password & Group IF GroupCount < 3 THEN Password ← Password & "-" ENDIF NEXT GroupCount RETURN Password ENDFUNCTION Password declared as a STRING and set to the empty string "" [1] Outer loop that runs three times, once for each group [1] Inner loop that runs four times, once for each character in a group [1] Uses RandomChar() and Exists() together inside a loop [1] Conditional loop (REPEAT ... UNTIL) that repeats until the character is not already in the group [1] Unique character joined onto Group, and each finished Group joined onto Password [1] Hyphens put between the groups (or the last one removed, for example with LEFT) and Password returned [1] Other correct methods also score, for example removing the final hyphen afterwards. (b) First use FindPassword() to check the website is not already stored. Then look for an unused row (column 1 is the empty string "") and write the name and the encrypted password there. FUNCTION AddPassword(Name : STRING, Password : STRING) RETURNS BOOLEAN DECLARE Index : INTEGER DECLARE Added : BOOLEAN Added ← FALSE Index ← 1 IF FindPassword(Name) = "" THEN WHILE Added = FALSE AND Index <= 500 IF Secret[Index, 1] = "" THEN Secret[Index, 1] ← Name Secret[Index, 2] ← Encrypt(Password) Added ← TRUE ELSE Index ← Index + 1 ENDIF ENDWHILE ENDIF RETURN Added ENDFUNCTION Checks the domain name is not already stored, using FindPassword() (or a linear search) [1] Conditional loop that stops when the item is added or the end of the array is reached [1] Tests column 1 of each row for an unused element (the empty string "") inside the loop [1] If unused, writes the name to column 1 and the password to column 2, and sets a flag [1] Password stored in encrypted form by using Encrypt() [1] Returns the BOOLEAN value correctly in every case (TRUE if added, otherwise FALSE) [1] (c) Use one line per website, with a separator so the two parts can be split apart again. Choose a separator character that can never appear in a website domain name, for example a space [1] Build one string: the domain name followed by the separator [1] Join the encrypted password on the end and write the whole string as one line of the file [1] Another accepted way: turn the length of the domain name into a fixed-length string, then join that length string, the domain name and the encrypted password into one line. KEY IDEA Use loops with a flag or condition to build and search data, and reuse the given modules (RandomChar, Exists, FindPassword, Encrypt) rather than rewriting them. TOTAL 16 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Digify ID. Tap the picture to open it on YouTube.