9618/22/O/N/21 question 6: worked solution (October/November 2021, paper 22)

Show as plain text

Cambridge AS Level Computer Science 9618 · 10.2 Arrays Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4. Q14 9618/22/O/N/21 original Q6 Structured · 19 marks Worked solution (a) Search row by row, and in each row column by column. Use a flag so both loops stop as soon as the first 1 is found. FUNCTION FirstRowSet() RETURNS INTEGER DECLARE Row, Col : INTEGER DECLARE Found : BOOLEAN Found ← FALSE Row ← 1 WHILE Row <= 800 AND Found = FALSE Col ← 1 WHILE Col <= 1280 AND Found = FALSE IF Screen[Row, Col] = 1 THEN Found ← TRUE ENDIF Col ← Col + 1 ENDWHILE Row ← Row + 1 ENDWHILE IF Found = FALSE THEN RETURN -1 ELSE RETURN Row - 1 ENDIF ENDFUNCTION Function heading and ending with the return type INTEGER [1] Outer loop through the rows 1 to 800 [1] Inner loop through the columns 1 to 1280 (or 1280 to 1) [1] Screen element referenced and tested for = 1 (or <> 0) [1] If it is 1, the row number is saved and the loops are exited [1] Index variables of both loops incremented [1] Row number returned, or -1 if nothing was found [1] Row - 1 is returned because Row was increased once after the 1 was found. (b) Write two points: A flag is used to exit the loops (iteration is stopped) [1] as soon as a Screen element with value 1 is found, so the rest of the array is not searched [1] (c)(i) Write any two of these: Parameters are passed to the module to say which search to do (first or last, row or column) [1] The search is controlled by (global) variables or parameters, so the loop start, end and direction change [1] Other points that also score: instead of the two points above, the search algorithms of the four old modules are put into the new module, and the new module returns or stores the four results. (c)(ii) Write one advantage and one disadvantage. Advantage (any one): only one module to change if the specification changes, less repeated code, or easier to reuse [1] Disadvantage (any one): the single module is more complex, more error prone or harder to debug, or it cannot be split between programmers [1] (d) Call the four modules to get the edges, check for no touch, then take the middle of each pair. PROCEDURE GetCentre() DECLARE StartRow, EndRow, StartCol, EndCol : INTEGER StartRow ← FirstRowSet() IF StartRow = -1 THEN CentreRow ← -1 ELSE EndRow ← LastRowSet() StartCol ← FirstColSet() EndCol ← LastColSet() CentreRow ← INT((StartRow + EndRow) / 2) CentreCol ← INT((StartCol + EndCol) / 2) ENDIF ENDPROCEDURE One of the Set modules called and its result checked for -1 (no element set) [1] If it is -1, CentreRow set to -1 [1] All four Set modules called to get the edge values [1] Centre row and centre column calculated as the middle of the first and last values [1] INT() or DIV used on the calculated values [1] Calculated values assigned to CentreRow and CentreCol [1] KEY IDEA Stop searching with a flag as soon as the first 1 is found, and find the centre by averaging the first and last row and column. TOTAL 19 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by CS BY AQIB KHAN. Tap the picture to open it on YouTube.