Cambridge AS Level Computer Science 9618 · 10.2 Arrays

Q16 · 9618/21/M/J/22 Q8

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

Q169618/21/M/J/22original Q8Structured · 21 marks
Question 8 from 9618/21/M/J/22

Worked solution

(a)

Pick one of the three ranges at random, then make a character in that range. Use ASCII: 97 to 122 for lower case, 65 to 90 for upper case, 48 to 57 for digits.

FUNCTION RandomChar() RETURNS CHAR
    DECLARE ThisRange, Code : INTEGER
    DECLARE ThisChar : CHAR
    ThisRange ← INT(RAND(3)) + 1
    CASE OF ThisRange
        1 : Code ← INT(RAND(26)) + 97
            ThisChar ← CHR(Code)
        2 : Code ← INT(RAND(26)) + 65
            ThisChar ← CHR(Code)
        3 : Code ← INT(RAND(10)) + 48
            ThisChar ← CHR(Code)
    ENDCASE
    RETURN ThisChar
ENDFUNCTION

A random integer generated (RAND with INT) [1]

The range is chosen at random from the three ranges [1]

A selection structure (CASE or IF) based on the chosen range [1]

One alphanumeric range correct [1]

All three ranges correct [1]

The character is returned [1]

(b)

Search the first column for the domain name. When found, decrypt the password in column 2. Stop when found or at the end of the array.

FUNCTION FindPassword(Name : STRING) RETURNS STRING
    DECLARE Index : INTEGER
    DECLARE Password : STRING
    Password ← ""
    Index ← 1
    WHILE Password = "" AND Index <= 500
        IF Secret[Index, 1] = Name THEN
            Password ← Decrypt(Secret[Index, 2])
        ELSE
            Index ← Index + 1
        ENDIF
    ENDWHILE
    IF Password = "" THEN
        OUTPUT "Domain name not found"
    ENDIF
    RETURN Password
ENDFUNCTION

All local variables declared [1]

Conditional loop that stops when found or at the end of the array [1]

Column 1 element compared with the parameter [1]

Decrypt() used with the column 2 element as its parameter [1]

The value returned by Decrypt() is used (stored and returned) [1]

A warning message output if the domain name is not found [1]

A STRING value returned (empty string if not found) [1]

(c)

Name the method and describe it.

Method: stub testing [1]

Description: write a simple module in place of each unfinished module, so the main program can be run [1]

The simple module returns an expected value, or outputs a message to show it was called [1]

(d)

Each password should break a different rule. Two examples:

Password 1: "FxAf-3haV-Tq4" (too short, only 13 characters) [1]

Password 2: "FxAf-3haV-Tq4x" (14 characters but the 'x' is duplicated) [1]

Other correct choices break a different rule, for example an invalid character, wrong grouping or wrong number of hyphens. One valid password is also accepted for one mark.

(e)

Change how the range is chosen.

Generate a random integer that can be split into three equal parts (for example a number from 1 to 3) [1]

One third of the values means numeric (digits) [1]

The other values (two thirds) mean an alphabetic character, so letters come twice as often [1]

KEY IDEABuild the character from a random range choice and ASCII codes, and weight the range choice to change how often each type appears.
TOTAL 21 marks

Official mark scheme

Mark scheme for 9618/21/M/J/22 question 8
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.