9618/23/O/N/22 question 5: worked solution (October/November 2022, paper 23)
Show as plain textHide plain text
Cambridge AS Level Computer Science 9618 · 11.3 Structured Programming Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 3 of 3. Q76 9618/23/O/N/22 original Q5 Structured · 17 marks Worked solution (a)(i) 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) ENDPROCEDURE Procedure 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] (a)(ii) 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). (b)(i) Use dot notation for a field of a record: LineData.Cost ← 12.99 Correct statement [1] (b)(ii) 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). (c) 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] (d) 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). KEY IDEA Use BYREF for results that must reach the caller, and a walkthrough with a trace table finds errors by watching variable values change. TOTAL 17 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Zafar Ali Khan (ZAK). Tap the picture to open it on YouTube.