A mobile phone has a touchscreen. The screen is represented by a grid, divided into 800 rows and 1280 columns. The grid is represented by a 2D array Screen of type INTEGER. An array element will be set to 0 unless the user touches that part of the screen. Many array elements are set to 1 by a single touch of a finger or a stylus. The following diagram shows a simplified touchscreen. The dark line represents a touch to the screen. All grid elements that are wholly or partly inside the outline will be set to 1. These elements are shaded. The element shaded in black represents the centre point. [Diagram showing a grid with a shaded oval touch, a black shaded centre at row 6, col 11, with axes labeled 6 and 11] A program is needed to find the coordinates (the row and column) of the centre point. The centre point on the diagram above is row 6, column 11. Assume: • the user may only touch one area at a time • screen rotation does not affect the touchscreen. The programmer has started to define program modules as follows: Module Description SetRow() • Called with three parameters of type INTEGER: (generates test • a row number data) • the number of pixels to be skipped starting from column 1 • the number of pixels that should be set to 1 • Sets the required number of pixels to 1 For example, SetRow(3, 8, 5) will give row 3 as in the diagram shown. SearchInRow() • Takes two parameters of type INTEGER: • a row number • a start column (1 or 1280) • Searches the given row from the start column (either left to right or right to left) for the first column that contains an element set to 1 • Returns the column number of the first element in the given row that is set to 1 • Returns –1 if no element is set to 1 SearchInCol() • Takes two parameters of type INTEGER: • a column number • a start row (1 or 800) • Searches the given column from the start row (either up or down) for the first row that contains an element set to 1 • Returns the row number of the first element in the given column that is set to 1 • Returns –1 if no element is set to 1
- (a)Write pseudocode to implement the module SetRow().[5]
- (b)The module description of SearchInRow() is provided here for reference. Module Description SearchInRow() • Takes two parameters of type INTEGER: • a row number • a start column (1 or 1280) • Searches the given row from the start column (either left to right or right to left) for the first column that contains an element set to 1 • Returns the column number of the first element in the given row that is set to 1 • Returns –1 if no element is set to 1 Write pseudocode to implement the module SearchInRow().[8]
- (c)The following new module is introduced: Module Description GetCentreCol() • Called with a row number as an INTEGER • Uses SearchInRow() to find the first and last columns in the given row which have an array element set to 1 • Returns the index of the column midway between the first and last columns • Returns –1 if no element is set to 1 Write pseudocode to implement the module GetCentreCol().[6]
This question uses a diagram from the original paper, which isn't shown here. Open the full paper from the past papers archive to see it.
Mark scheme
Marking points from the official mark scheme (Oct/Nov 2021). The mark or mark type is shown in brackets.
- [5] PROCEDURE SetRow(Row, SkipNum, SetNum : INTEGER)
- DECLARE Col : INTEGER
- // array is 1280 x 800
- FOR Col ← SkipNum + 1 TO SkipNum + SetNum
- Screen[Row, Col] ← 1
- NEXT Index
- ENDPROCEDURE
- ALTERNATIVE 1:
- FOR Col ← 1 TO SetNum
- Screen[Row, SkipNum + Col] ← 1
- NEXT Col
- ALTERNATIVE 2:
- WHILE SetNum > 0
- Screen[Row, SkipNum + SetNum] ← 1
- SetNum ← SetNum - 1
- ENDWHILE
- Mark as follows:
- 1 Procedure heading and ending including parameters
- 2 Declaration of local Integer for Col
- 3 Count-controlled loop with meaningful start number
- 4 correct stop number
- 5 Reference Screen Array element and set to 1 in a loop
- [8] FUNCTION SearchInRow(ThisRow, StartCol : INTEGER) RETURNS INTEGER
- DECLARE ThisCol, Step : INTEGER
- DECLARE Found: BOOLEAN
- // array is 1280 x 800
- Found ← FALSE
- ThisCol ← StartCol
- // first decide which way to search
- IF StartCol = 1 THEN
- Step ← 1
- EndCol ← 1281
- ELSE
- Step ← -1
- EndCol ← 0
- ENDIF
- WHILE ThisCol <> EndCol AND Found = FALSE
- IF Screen[ThisRow, ThisCol] <> 1 THEN
- ThisCol ← ThisCol + Step
- ELSE
- Found ← TRUE
- ENDIF
- ENDWHILE
- IF Found = FALSE THEN
- ThisCol ← -1
- ENDIF
- RETURN ThisCol
- ENDFUNCTION
- Mark as follows:
- 1 Interpreting StartCol parameter to determine direction of search
- 2 An attempt at searching both up and down
- 3 Conditional Loop / Count-controlled loop with use of ThisCol index
- 4 Using correct values for StartCol, EndCol and Step
- 5 Reference a Screen element and compare with 1 in a loop
- 6 If equal save column or immediately Return column in a loop
- 7 Return column number or -1
- Loop(s) terminate when element with value = 1 found
- Max 7 marks if function heading, including return type, and ending is incorrect or incomplete
- [6] FUNCTION GetCentreCol (ThisRow : INTEGER) RETURNS INTEGER
- DECLARE StartCol, EndCol, CentreCol : INTEGER
- StartCol ← SearchInRow(ThisRow, 1)
- IF StartCol = -1 THEN
- CentreCol ← StartCol
- ELSE
- EndCol ← SearchInRow(ThisRow, 1280)
- CentreCol ← INT((StartCol + EndCol)/2)
- ENDIF
- RETURN CentreCol
- ENDFUNCTION
- Mark as follows:
- 1 Declaration of local INTEGER for return value
- 2 Use SearchInRow() with correct parameters and check for -1
- 3 Use SearchInRow(ThisRow, 1) and SearchInRow(ThisRow, 1280)
- 4 Calculate centre column
- 5 Use of INT () function // use of DIV
- 6 Return -1 or centre value
- Max 5 marks if function heading, including return type, and ending is incorrect or incomplete
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 →