A computer shop assembles desktop computers, using items bought from several suppliers. A text file Stock.txt contains information about each item. Information for each item is stored as a single line in the Stock.txt file in the format: <ItemNum><SupplierCode><Description> Item information is as follows: | | Format | Comment | |:---|:---|:---| | ItemNum | 4 numeric characters | unique number for each item in the range "0001” to "5999" inclusive | | SupplierCode | 3 alphabetic characters | code to identify the supplier of the item | | Description | a string | a minimum of 12 characters | The file is organised in ascending order of ItemNum and does not contain all possible values in the range. The programmer has defined the first program module as follows: | Module | Description | |:---|:---| | ChangeSupp() | • called with two parameters Code1 and Code2 of type string that represent valid supplier codes | • creates a new file NewStock.txt from the contents of the file Stock.txt where any reference to Code1 is replaced by Code2 | • returns a count of the number of items that have had their supplier code changed |
- (a)Write pseudocode for module ChangeSupp().[8]
- (b)A new module is required: | Module | Description | |:---|:---| | Report_1() | • takes a parameter of type string that represents a SupplierCode | • searches the Stock.txt file for each line of item information that contains the given SupplierCode | • produces a formatted report of items for the given SupplierCode, for example, for supplier DRG, the output could be: Report for Supplier: DRG Item | Description ---|--- 1234 | USB Printer Cable 3m 1273 | 32GB USB Flash Drive 1350 | Mouse Mat 320 x 240mm Number of items listed: 3 Write pseudocode for module Report_1().[6]
- (c)(i)The format of the output from module Report_1() from part (b) is changed. The number of items listed is moved to the top of the report as shown in the example: Report for Supplier: DRG Number of items listed: 3 Item | Description ---|--- 1234 | USB Printer Cable 3m 1273 | 32GB USB Flash Drive 1350 | Mouse Mat 320 x 240mm Explain why this new layout would increase the complexity of the algorithm.[2]
- (c)(ii)The algorithm will be modified to produce the report in the new format. The modified algorithm will be implemented so that the file Stock.txt is only read once. Describe the modified algorithm.[3]
Mark scheme
Marking points from the official mark scheme (May/June 2023). The mark or mark type is shown in brackets.
- [8] FUNCTION ChangeSupp(Code1, Code2 : STRING) RETURNS INTEGER
- [8] DECLARE Count : INTEGER
- [8] DECLARE ThisLine, ThisCode : STRING
- [8] OPENFILE "Stock.txt" FOR READ
- [8] OPENFILE "NewStock.txt" FOR WRITE
- [8] Count ← 0
- [8] WHILE NOT EOF("Stock.txt")
- [8] READFILE("Stock.txt ", ThisLine) // brackets optional
- [8] ThisCode ← MID(ThisLine, 5, 3)
- [8] IF ThisCode = Code1 THEN
- [8] ThisLine ← LEFT(ThisLine, 4) & Code2
- [8] & RIGHT(ThisLine, LENGTH(ThisLine) - 7)
- [8] Count ← Count + 1
- [8] ENDIF
- [8] WRITEFILE("NewStock.txt", ThisLine) // brackets optional
- [8] ENDWHILE
- [8] CLOSEFILE "NewStock.txt"
- [8] CLOSEFILE "Stock.txt"
- [8] RETURN Count
- [8] ENDFUNCTION
- [8] MP1 Open both files, in correct modes, and subsequently close
- [8] MP2 Conditional loop until EOF (“Stock.txt")
- [8] MP3 Read a line from Stock.txt AND extract ThisCode in a loop
- [8] MP4 Test ThisCode = Code1 AND if true, increment Count (must have been Initialised) in a loop
- [8] MP5 Update ThisLine using substring functions and '&' in a loop
- [8] MP6 completely correct update of ThisLine in a loop
- [8] MP7 Write ThisLine to NewStock.txt in a loop
- [8] MP8 Return count after loop
- [6] PROCEDURE Report_1(Supp : STRING)
- [6] DECLARE Count : INTEGER
- [6] DECLARE ThisItemNum, ThisDesc, ThisLine, ThisCode : STRING
- [6] Count ← 0
- [6] OPENFILE "Stock.txt" FOR READ
- [6] OUTPUT "Report for Supplier:" & Supp
- [6] OUTPUT "" //Blank line as per example
- [6] OUTPUT "Item Description"
- [6] OUTPUT "" //Blank line as per example
- [6] WHILE NOT EOF("Stock.txt")
- [6] READFILE("Stock.txt", ThisLine)
- [6] ThisCode ← Mid(ThisLine, 5, 3)
- [6] IF ThisCode = Supp THEN
- [6] ThisItemNum ← LEFT(ThisLine, 4)
- [6] ThisDesc ← RIGHT(ThisLine, LENGTH(ThisLine) - 7)
- [6] OUTPUT ThisItem & " " & ThisDesc
- [6] Count ← Count + 1
- [6] ENDIF
- [6] ENDWHILE
- [6] CLOSEFILE "Stock.txt"
- [6] OUTPUT "" //Blank line as per example
- [6] OUTPUT "Number of items listed: ", Count
- [6] ENDPROCEDURE
- [6] MP1 Output report header (blank lines optional) – Must contain the parameter code
- [6] MP2 Conditional loop until EOF("Stock.txt")
- [6] MP3 Read a line from Stock.txt AND extract SupplierCode in a loop
- [6] MP4 Test if SupplierCode = Supp then increment count (must have been Initialised)
- [6] MP5 Extract AND output item and description in a loop
- [6] MP6 Output the final line with count
- [2] Max 2 marks
- [2] MP1 Must 'calculate' the count before any item + description output / after the file is read once
- [2] MP2 Lines to be output have to be stored ...
- [2] MP3 The file has to be read twice
- [3] One mark per point:
- [3] MP1 Loop through the file calculating the count
- [3] MP2 Save 'selected' items in an array
- [3] MP3 (After all lines have been read), output the header lines / count
- [3] MP4 Loop through the array to output each array element
Write your answer and get it marked
On Oracle Prep you can write or photograph your own answer and have it marked by AI against this mark scheme, with feedback on what you missed.
Sign up free →