Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.
Check the length first, then SuppExists(), then check every character is a letter (upper or lower case).
FUNCTION IsNewSupp(ThisString : STRING) RETURNS BOOLEAN
DECLARE Index : INTEGER
DECLARE ThisChar : CHAR
IF LENGTH(ThisString) <> 5 THEN
RETURN FALSE
ENDIF
IF SuppExists(ThisString) THEN
RETURN FALSE
ENDIF
FOR Index ← 1 TO 5
ThisChar ← TO_LOWER(MID(ThisString, Index, 1))
IF ThisChar < 'a' OR ThisChar > 'z' THEN
RETURN FALSE
ENDIF
NEXT Index
RETURN TRUE
ENDFUNCTIONCheck ThisString is exactly 5 characters long [1]
Use SuppExists() with a 5-character string as the parameter [1]
Loop for 5 iterations (the length of the string) [1]
Extract a character inside the loop [1]
Test whether the character is alphabetic inside the loop [1]
...allowing for both upper and lower case [1]
Return a Boolean after a reasonable attempt [1]
Read the file line by line and stop as soon as the item is found or the ItemNum read is bigger than the new one, because the file is in order.
FUNCTION CheckNewItem(NewLine : STRING) RETURNS BOOLEAN
DECLARE NotFound : BOOLEAN
DECLARE NewItemNum, ThisItemNum, ThisLine : STRING
NotFound ← TRUE
OPENFILE "Stock.txt" FOR READ
NewItemNum ← LEFT(NewLine, 4)
ThisItemNum ← "0000"
WHILE NOT EOF("Stock.txt") AND NotFound = TRUE AND ThisItemNum < NewItemNum
READFILE "Stock.txt", ThisLine
ThisItemNum ← LEFT(ThisLine, 4)
IF ThisItemNum = NewItemNum THEN
NotFound ← FALSE
ENDIF
ENDWHILE
CLOSEFILE "Stock.txt"
RETURN NotFound
ENDFUNCTIONOpen Stock.txt in READ mode and close it afterwards [1]
Extract NewItemNum from the parameter [1]
Conditional loop until EOF("Stock.txt") [1]
...or until the ItemNum is found [1]
...or until ThisItemNum is not less than NewItemNum (efficient: stops early) [1]
Read a line and extract ThisItemNum inside the loop [1]
If ThisItemNum = NewItemNum, end the loop / set a flag, then return a Boolean after a reasonable attempt [1]
Maximum 6 if the function heading and ending are missing or wrong.
Write: integration testing [1]
Use stub testing.
Write a simple dummy module to replace the module that does not work properly [1]
The dummy module returns an expected value (or outputs a message to show it was called) [1]
Write: append [1]
Because the file is in order, the search does not have to read every line.
The search can stop early [1]
if the current value (line) read from the file [1]
is greater than the value being searched for [1]
▶ Starts at Q8 (59:45)
Starts at Q8 (59:45) · 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.