7 A teacher is designing a program to perform simple syntax checks on programs written by students. Two global 1D arrays are used to store the syntax error data. Both arrays contain 500 elements. • Array ErrCode contains integer values that represent an error number in the range 1 to 800. • Array ErrText contains string values that represent an error description. The following diagram shows an example of the arrays. Index ErrCode ErrText 1 10 "Invalid identifier name" 2 20 "Bracket mismatch" 3 50 "Undeclared variable" 4 60 "Type mismatch in assignment" ... 500 999 <Undefined> Note: • There may be less than 500 error numbers so corresponding elements in both arrays may be unused. Unused elements in ErrCode have the value 999. The value of unused elements in ErrText is undefined. • Values in the ErrCode array are stored in ascending order but not all values may be present, for example, there may be no error code 31. The teacher has defined two program modules as follows: Module Description OutputError() • takes two parameters as integers: o a line number in the student's program o an error number • searches for the error number in the ErrCode array: o if found, outputs the corresponding error description and the line number, for example: "Bracket mismatch on line 34" o if not found, outputs the line number and a warning, for example: "Unknown error on line 34" SortArrays() • sorts the arrays into ascending order of ErrCode
- (a)Write efficient pseudocode for module OutputError().[6]
- (b)Write an efficient bubble sort algorithm in pseudocode for module SortArrays().[8]
- (c)(i)State the error in the record declaration. TYPE ErrorRec DECLARE ErrCode : STRING DECLARE ErrText : STRING ENDTYPE[1]
- (c)(ii)State two benefits of using the single array of the user-defined data type. 1 2[2]
- (c)(iii)Write the declaration for the single array in pseudocode.[1]
Mark scheme
Marking points from the official mark scheme (Oct/Nov 2022). The mark or mark type is shown in brackets.
- [6] (a) One mark per point (Max 6): 1 Procedure heading and ending including parameters 2 Conditional loop containing incrementing Index... 3 ...terminating when ErrNum found 4 ...terminating when ErrCode[Index] > ErrNum (i.e. ErrNum not found) OR after element 500 tested 5 Test if found and OUTPUT 'Found' message 6 ...otherwise OUTPUT 'Not Found' message PROCEDURE OutputError(LineNum, ErrNum : INTEGER) DECLARE Index : INTEGER Index <- 0 // Search until ErrNum found OR not present OR end of array REPEAT Index <- Index + 1 UNTIL ErrCode[Index] >= ErrNum OR Index = 500 IF ErrCode[Index] = ErrNum THEN OUTPUT ErrText[Index], " on line ", LineNum //Found ELSE //Not found OUTPUT "Unknown error on line ", LineNum ENDIF ENDPROCEURE
- [8] (b) One mark per point (Max 8): 1 Procedure heading and ending as shown 2 Conditional loop correctly terminated 3 An inner loop 4 Correct range for inner loop 5 Comparison (element J with J+1) in a loop 6 Swap elements in both arrays in a loop 7 'No-Swap' mechanism: Conditional outer loop including flag reset // Flag set in inner loop to indicate swap 8 Efficiency (this scenario): terminate inner loop when ErrCode = 999 9 Reducing Boundary in the outer loop PROCEDURE SortArrays() DECLARE TempInt, J, Boundary : INTEGER DECLARE TempStr : STRING DECLARE NoSwaps : BOOLEAN Boundary <- 499 REPEAT NoSwaps <- TRUE FOR J <- 1 TO Boundary IF ErrCode[J] > ErrCode[J+1] THEN //first swap ErrCode elements TempInt <- ErrCode[J] ErrCode[J] <- ErrCode[J+1] ErrCode[J+1] <- TempInt //now swap corresponding ErrText elements TempStr <- ErrText[J] ErrText[J] <- ErrText[J+1] ErrText[J+1] <- TempStr NoSwaps <- FALSE ENDIF NEXT J Boundary <- Boundary - 1 UNTIL NoSwaps = TRUE ENDPROCEDURE
- [1] (c)(i) ErrCode should be an INTEGER // ErrCode should not be a STRING
- [2] (c)(ii) Benefits include: 1 Array of records can store mixed data types / multiple data types under a single identifer 2 Tighter / closer association between ErrCode and ErrText // simpler code as fields can be referenced together // values cannot get out of step as with two arrays 3 Program easier to design / write / debug / test / maintain / understand One mark per point Note: Max 2 marks
- [1] (c)(iii) DECLARE Error : ARRAY[1:500] OF ErrorRec
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 →