Cambridge AS Level Computer Science 9618 · 10.2 Arrays

Q18 · 9618/23/M/J/22 Q9

Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4.

Q189618/23/M/J/22original Q9Structured · 16 marks
Question 9 from 9618/23/M/J/22

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 IDEAUse 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

Mark scheme for 9618/23/M/J/22 question 9
VIDEO
▶ Whole paper

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