Cambridge AS Level Computer Science 9618 · 10.2 Arrays

Q25 · 9618/22/O/N/23 Q8

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

Q259618/22/O/N/23original Q8Structured · 18 marks
Question 8 from 9618/22/O/N/23

Worked solution

(a)

Turn the ID into a number, then step through the rows. Skip unused rows (column 1 is -1). If the number is inside a range, store that row's port and stop. Return the port, which stays -1 if nothing matched.

FUNCTION GetPort(ThisDest : STRING) RETURNS INTEGER DECLARE Index, DNum, Port : INTEGER DNum ← STR_TO_NUM(ThisDest) Index ← 1 Port ← -1 REPEAT IF RouteTable[Index, 1] <> -1 THEN IF DNum >= RouteTable[Index, 1] AND DNum <= RouteTable[Index, 2] THEN Port ← RouteTable[Index, 3] ENDIF ENDIF Index ← Index + 1 UNTIL Index = 7 OR Port <> -1 RETURN Port ENDFUNCTION

Function heading and ending with the parameter and return type [1]

Parameter converted to a number with STR_TO_NUM() [1]

Loop through the rows of the array, with a way of stopping [1]

Unused rows (column 1 = -1) skipped inside the loop [1]

Each range tested correctly, with the destination at or above column 1 and at or below column 2 [1]

Port value stored when the destination matches, and the loop exited [1]

Port value returned, including -1 if no match is found [1]

Other points that also score: an attempt to check one range against the destination.

Another correct way also scores, for example six separate IF statements, one per row.

(b)

Ignore messages with no data (fewer than 4 characters), take the first 3 characters as the destination, choose stack 1 or 2, call StackMsg() and output a warning if it returns FALSE.

PROCEDURE ProcessMsg(ThisMsg : STRING) DECLARE ThisDest : STRING DECLARE Response : BOOLEAN DECLARE StackNum : INTEGER IF LENGTH(ThisMsg) >= 4 THEN ThisDest ← LEFT(ThisMsg, 3) IF ThisDest = MyID THEN StackNum ← 1 ELSE StackNum ← 2 ENDIF Response ← StackMsg(ThisMsg, StackNum) IF Response = FALSE THEN OUTPUT "Message discarded - no room on stack" ENDIF ENDIF ENDPROCEDURE

Message ignored if the data field is empty [1]

Destination ID extracted from the message [1]

Test whether the destination is this computer (MyID) [1]

An attempt to use StackMsg() [1]

StackMsg() used correctly for both stacks (1 and 2) [1]

Return value of StackMsg() tested for both cases [1]

An error or warning output if StackMsg() returns FALSE [1]

(c)(i)

Write any three points for one scenario. The first one is shown here.

Circumstances: more than one line is stored on the stack before lines are removed [1]

A stack is a last in, first out (LIFO) structure, so the last line added is the first one out [1]

So the lines are written to the file out of sequence [1]

Other accepted scenarios: the stack is full so some lines are lost, or elements are too small so only part of each line is stored, or the stack is empty so blank lines are written.

(c)(ii)

A queue, because it is first in, first out, so lines come out in the order they arrived. [1]

KEY IDEASearch ranges in a loop that skips unused rows, and remember a stack reverses order while a queue keeps it.
TOTAL 18 marks

Official mark scheme

Mark scheme for 9618/22/O/N/23 question 8
VIDEO
▶ Starts at Q8 (45:53)

Starts at Q8 (45:53) · 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.