Cambridge AS Level Computer Science 9618 · 10.2 Arrays

Q15 · 9618/23/O/N/21 Q6

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

Q159618/23/O/N/21original Q6Structured · 19 marks
Question 6 from 9618/23/O/N/21

Worked solution

(a)

Skip SkipNum pixels, then set SetNum pixels to 1 in the given row.

PROCEDURE SetRow(Row, SkipNum, SetNum : INTEGER)
    DECLARE Col : INTEGER
    FOR Col ← SkipNum + 1 TO SkipNum + SetNum
        Screen[Row, Col] ← 1
    NEXT Col
ENDPROCEDURE

Procedure heading and ending with the three INTEGER parameters [1]

Local INTEGER variable declared for the column [1]

Count-controlled loop with a sensible start value (SkipNum + 1) [1]

Correct stop value (SkipNum + SetNum) [1]

Screen element referenced 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 counting SetNum down.

(b)

The start column decides the direction: 1 means left to right, 1280 means right to left. Stop as soon as a 1 is found.

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
            Found ← TRUE
        ELSE
            ThisCol ← ThisCol + Step
        ENDIF
    ENDWHILE
    IF Found = FALSE THEN
        ThisCol ← -1
    ENDIF
    RETURN ThisCol
ENDFUNCTION

StartCol parameter used to decide the direction of the search [1]

An attempt at searching in both directions [1]

Conditional or count-controlled loop using the column index ThisCol [1]

Correct values used for the start column, end column and step [1]

Screen element in the row referenced and compared with 1 inside the loop [1]

If equal, column saved or returned straight away [1]

The loop stops as soon as an element with value 1 is found [1]

Column number returned, or -1 if none found [1]

Marks are lost if the function heading, return type or ending is wrong.

(c)

Search from the left for the first column and from the right for the last, then 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
ENDFUNCTION

Local INTEGER variable declared for the value to return [1]

SearchInRow() used with correct parameters and the result checked for -1 [1]

Both SearchInRow(ThisRow, 1) and SearchInRow(ThisRow, 1280) used [1]

Centre column calculated from the first and last columns [1]

INT() or DIV used [1]

-1 or the centre value returned [1]

KEY IDEAChoose the search direction from the start column, stop at the first 1, and find a centre by averaging the first and last positions.
TOTAL 19 marks

Official mark scheme

Mark scheme for 9618/23/O/N/21 question 6
VIDEO
▶ Topic lesson

A lesson on this topic · by ZAINEMATICS. 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.