Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 3 of 3.
The three results must change the caller's variables, so pass them by reference. The cost is everything after the first 37 characters (5 + 32).
PROCEDURE Unpack(BYVAL TLine : STRING, BYREF SID, SDesc, SCost : STRING)
SID ← LEFT(TLine, 5)
SDesc ← MID(TLine, 6, 32)
SCost ← RIGHT(TLine, LENGTH(TLine) - 37)
ENDPROCEDUREProcedure heading and ending with four parameters [1]
BYREF used for the three extracted values [1]
StockID extracted (first 5 characters) and assigned [1]
Description extracted (32 characters from position 6) and assigned [1]
Length of the cost part calculated as the remainder of the string [1]
Cost extracted using that length and assigned [1]
Any two of these:
The procedure interface is how the calling program passes data to and from the procedure [1]
For Unpack() it defines the four parameters [1]
It gives their data types and order (also scores, but only two marks are available).
Use dot notation for a field of a record:
LineData.Cost ← 12.99
Correct statement [1]
Any two of these:
The new function must return an item of type StockItem [1]
A local variable of type StockItem is needed to build the result [1]
The Cost field must be converted from a string to a real number (also scores, but only two marks are available).
Reason: meaningful names make the code easier to understand (they show the purpose), so it is easier to debug, test and maintain [1]
Example 1: use indentation (or white space) [1]
Example 2: use comments (or keywords in capitals, or local variables) [1]
Any three of these:
A walkthrough goes through the program one line at a time, using a trace table [1]
It records the values of the variables as they change [1]
An error shows when a variable gets an unexpected value [1]
An error also shows when the program takes an unexpected path through the code (also scores, but only three marks are available).
▶ Whole paper
Video solution of the whole paper · by Zafar Ali Khan (ZAK). 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.