Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4.
Skip SkipNum columns, then set SetNum pixels to 1 in the given row. The first column to set is SkipNum + 1 and the last is SkipNum + SetNum:
PROCEDURE SetRow(Row, SkipNum, SetNum : INTEGER)
DECLARE Col : INTEGER
FOR Col ← SkipNum + 1 TO SkipNum + SetNum
Screen[Row, Col] ← 1
NEXT Col
ENDPROCEDUREProcedure heading with the three parameters, and ENDPROCEDURE [1]
Local INTEGER declared for the column counter [1]
Count-controlled loop with a sensible start value (SkipNum + 1) [1]
Correct stop value (SkipNum + SetNum) [1]
Screen element referenced with the row and column and set to 1, inside the loop [1]
Other correct loops also score, for example FOR Col ← 1 TO SetNum with Screen[Row, SkipNum + Col], or a WHILE loop.
If StartCol is 1, search left to right (step 1, stop at 1281). Otherwise search right to left (step -1, stop at 0). Stop when an element equals 1:
FUNCTION SearchInRow(ThisRow, StartCol : INTEGER) RETURNS INTEGER
DECLARE ThisCol, Step, EndCol : INTEGER
DECLARE Found : BOOLEAN
Found ← FALSE
ThisCol ← StartCol
IF StartCol = 1 THEN
Step ← 1
EndCol ← 1281
ELSE
Step ← -1
EndCol ← 0
ENDIF
WHILE ThisCol <> EndCol AND Found = FALSE
IF Screen[ThisRow, ThisCol] <> 1 THEN
ThisCol ← ThisCol + Step
ELSE
Found ← TRUE
ENDIF
ENDWHILE
IF Found = FALSE THEN
ThisCol ← -1
ENDIF
RETURN ThisCol
ENDFUNCTIONUses StartCol to decide the direction of the search [1]
Can search in both directions (left to right and right to left) [1]
Conditional or count-controlled loop that uses ThisCol as the index [1]
Correct values for the start column, end column and step [1]
Screen element in the given row compared with 1, inside the loop [1]
When equal, the column is saved (or returned at once) [1]
Column number returned, or -1 if no element is 1 [1]
Loop ends when an element equal to 1 is found [1]
Only 7 marks can be scored if the function heading, with its return type, or ENDFUNCTION is wrong.
Find the first column by searching from column 1. If it is -1 return -1. Otherwise find the last column by searching from 1280 and take the middle:
FUNCTION GetCentreCol(ThisRow : INTEGER) RETURNS INTEGER
DECLARE StartCol, EndCol, CentreCol : INTEGER
StartCol ← SearchInRow(ThisRow, 1)
IF StartCol = -1 THEN
CentreCol ← -1
ELSE
EndCol ← SearchInRow(ThisRow, 1280)
CentreCol ← INT((StartCol + EndCol) / 2)
ENDIF
RETURN CentreCol
ENDFUNCTIONLocal INTEGER declared for the value to return [1]
SearchInRow() called with the correct parameters and the result checked for -1 [1]
SearchInRow(ThisRow, 1) and SearchInRow(ThisRow, 1280) both used [1]
Centre column worked out as the middle of the first and last columns [1]
INT() (or DIV) used so the result is a whole number [1]
Returns -1 or the centre value [1]
▶ Whole paper
Video solution of the whole paper · by Mr Saem. 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.