9618/22/M/J/23 question 6: worked solution (May/June 2023, paper 22)
Show as plain textHide plain text
Cambridge AS Level Computer Science 9618 · 11.2 Constructs Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 2 of 3. Q45 9618/22/M/J/23 original Q6 Structured · 6 marks 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 IDEA Build 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 VIDEO ▶ Starts at Q6 (44:03) Starts at Q6 (44:03) · by Pseudocode Pro. Tap the picture to open it on YouTube.