Cambridge AS Level Computer Science 9618 · 11.2 Constructs

Q45 · 9618/22/M/J/23 Q6

Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 2 of 3.

Q459618/22/M/J/23original Q6Structured · 6 marks
Question 6 from 9618/22/M/J/23

Worked solution

Build the first and last line once, build the middle line once, then output them.

PROCEDURE Square(Dim : INTEGER)
    DECLARE Count : INTEGER
    DECLARE Digit, Edge, Middle : STRING
    Digit ← NUM_TO_STR(Dim)
    Edge ← ""
    FOR Count ← 1 TO Dim
        Edge ← Edge & Digit
    NEXT Count
    Middle ← Digit
    FOR Count ← 1 TO Dim - 2
        Middle ← Middle & "*"
    NEXT Count
    Middle ← Middle & Digit
    OUTPUT Edge
    FOR Count ← 1 TO Dim - 2
        OUTPUT Middle
    NEXT Count
    IF Dim <> 1 THEN
        OUTPUT Edge
    ENDIF
ENDPROCEDURE

Procedure heading and ending including the parameter [1]

Loop using the parameter that builds the first and last line [1]

First and last line built correctly (the digit repeated Dim times) [1]

Loop that builds the middle line (digit, then * characters, then digit) [1]

First line output, and the last line output again when Dim is not 1 [1]

All the middle lines output in a loop [1]

Correct output for the first two squares (1 and 2) also scores, but only six marks are available.

A CASE OF Dim with a fixed set of OUTPUT statements for each square also scores, as long as all squares are correct.

KEY IDEABuild each different line as a string once, then output the lines with loops so one procedure works for every size.
TOTAL 6 marks

Official mark scheme

Mark scheme for 9618/22/M/J/23 question 6
VIDEO
▶ Starts at Q6 (44:03)

Starts at Q6 (44:03) · by Pseudocode Pro. 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.