Cambridge AS Level Computer Science 9618 · 10.3 Files

Q52 · 9618/22/M/J/23 Q8

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

Q529618/22/M/J/23original Q8Structured · 17 marks
Question 8 from 9618/22/M/J/23

Worked solution

(a)

Check the length is at least 19 (4 + 3 + 12), then the first 4 characters are digits in the range, then use OnlyAlpha() on characters 5 to 7.

FUNCTION CheckInfo(NewLine : STRING) RETURNS BOOLEAN
    DECLARE ThisNum : STRING
    DECLARE Index : INTEGER
    IF LENGTH(NewLine) < 19 THEN
        RETURN FALSE
    ENDIF
    FOR Index ← 1 TO 4
        IF NOT IS_NUM(MID(NewLine, Index, 1)) THEN
            RETURN FALSE
        ENDIF
    NEXT Index
    ThisNum ← LEFT(NewLine, 4)
    IF ThisNum < "0001" OR ThisNum > "5999" THEN
        RETURN FALSE
    ENDIF
    IF NOT OnlyAlpha(MID(NewLine, 5, 3)) THEN
        RETURN FALSE
    ENDIF
    RETURN TRUE
ENDFUNCTION

Test the length of the parameter [1]

Extract the first 4 characters (ItemNum) [1]

Test that the first four characters are all numeric [1]

Test ItemNum is in the range "0001" to "5999" [1]

Extract characters 5 to 7 (SupplierCode) [1]

Use OnlyAlpha() with the extracted SupplierCode [1]

Return a Boolean correctly in all cases [1]

(b)

Copy each line from Stock.txt to NewStock.txt. Write the new line just before the first line with a bigger ItemNum, once only. If none is bigger, add it at the end.

PROCEDURE AddItem(NewLine : STRING)
    DECLARE NewItemNum, ThisItemNum, ThisLine : STRING
    OPENFILE "Stock.txt" FOR READ
    OPENFILE "NewStock.txt" FOR WRITE
    NewItemNum ← LEFT(NewLine, 4)
    WHILE NOT EOF("Stock.txt")
        READFILE "Stock.txt", ThisLine
        ThisItemNum ← LEFT(ThisLine, 4)
        IF ThisItemNum > NewItemNum THEN
            WRITEFILE "NewStock.txt", NewLine
            NewItemNum ← "9999"
        ENDIF
        WRITEFILE "NewStock.txt", ThisLine
    ENDWHILE
    IF NewItemNum <> "9999" THEN
        WRITEFILE "NewStock.txt", NewLine
    ENDIF
    CLOSEFILE "Stock.txt"
    CLOSEFILE "NewStock.txt"
ENDPROCEDURE

Open both files in the correct modes and close them afterwards [1]

Conditional loop until the end of Stock.txt [1]

Read a line and extract ThisItemNum inside the loop [1]

If ThisItemNum > NewItemNum, write NewLine to NewStock.txt [1]

...with a way of doing this once only [1]

Write the line read from Stock.txt to NewStock.txt [1]

Deal with the case where NewLine is the new last line [1]

An array-based method also scores (store every line in an array indexed by ItemNum, then write the non-blank elements).

(c)

Method: stub testing [1]

The faulty modules SuppExists() and CheckSupplier() are replaced by dummy modules (stubs) [1]

...which return a known result or contain an output statement to show they have been called [1]

KEY IDEAValidate data piece by piece, merge a new record into a sorted file as you copy it, and use stubs so testing can continue.
TOTAL 17 marks

Official mark scheme

Mark scheme for 9618/22/M/J/23 question 8
VIDEO
▶ Starts at Q8 (59:53)

Starts at Q8 (59: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.