Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 2 of 3.
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
ENDPROCEDUREProcedure 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.
▶ 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.