Question 8

OCR Computer Science November 2021 Algorithms and programming (H446-02)A level40 marks

On the specification

(a)(i)
2.2 Problem solving and programming
(a)(ii)
2.1 Elements of computational thinking
(a)(iii)
2.1 Elements of computational thinking
(b)(i)
1.4 Data types, data structures and algorithms2.2 Problem solving and programming
(b)(ii)
2.2 Problem solving and programming
(b)(iii)
2.2 Problem solving and programming
Question text

8 A Nonogram is a logic puzzle where a player needs to colour in boxes. The puzzle is laid out as a grid and each square needs to be either coloured black or left white. The numbers at the side of each row and column tells the player how many of the boxes are coloured in consecutively. Where a row has two or more numbers, there must be a white square between the coloured squares. 2 2 11 3 1 12 3 0 1 12 In this example: • the first column has 1 1, this means there must be two single coloured boxes in this column. There must be at least 1 white box between them. • the first row has 2, this means there must be two consecutively coloured boxes in the row. Juan is creating a program that will store a series of Nonograms for a user to play. The game will randomly select a puzzle and display the blank grid with the numbers for each row and column to the user. The user plays the game by selecting a box to change its colour. If the box is white it will change to black and if it is black it will change to white. The user can choose to check the answer at any point, and the game will compare the grid to the answers and tell the user if they have got it correct or not. (a) Juan is creating a structure diagram to design the game. (i) Complete the structure diagram by adding another layer for New game, Play game and Check answer. Nonogram Check New game Play game [3] (ii) A structure diagram is one method of showing the decomposition of a problem. Explain why decomposing a problem can help a developer design a solution. [2] (iii) Identify one input, one process and one output required for the game. Input Process Output [3] (b) Juan uses the structure diagram to create a modular program with a number of subroutines. The program will use two integer 2-dimensional arrays to store the puzzles: • puzzle(5,5) stores the solution • answerGrid(5,5) stores the user’s current grid. A 0 represents a white box and a 1 represents a black box. (i) Juan creates a function, countRow(), to count the number of coloured boxes in one row and return the number of consecutive coloured boxes in that row. If there is more than one set of coloured boxes in the row, these are joined together and the string is returned. For example, in the following grid countRow for row 0 will return "2" as a string, and countRow for row 2 will return "1 1" as a string. If there are no 1s in a row, then "0" is returned as a string. 11 0 0 0 1 1 0 0 0 1 0 1 11 1 0 0 1 0 0 0 Complete the pseudocode algorithm countRow(). 1 function countRow(puzzle:byref, rowNum:byval) 2 count = 0 3 output = " " 4 for i = 0 to 5 if puzzle[rowNum, i] == then 6 count = count + 1 7 elseif count >= 1 then 8 output = output + str( ) + " " 9 count = 0 10 endif 11 next i 12 if count>= 1 then 13 output=output+str(count) 14 elseif output == "" then 15 output = " " 16 endif 17 return 18 endfunction [5] (ii) Explain the purpose of line 03 in the function countRow. [2] (iii) Describe the purpose of branching and iteration in the function countRow. [3] (iv) The procedure displayRowAnswer() takes puzzle as a parameter and outputs the value in each box. Each box in a row is separated by a space. At the end of each row there are two spaces and (by calling the function countRow from part 8(b)(i)) the clue values for that row. For example the puzzle below: 11 0 0 0 1 1 0 0 0 1 0 1 11 1 0 0 1 0 0 0 Would output: 11 0 0 0 2 1 1 0 0 2 0 1 0 1 1 1 11 1 0 0 3 1 0 0 0 1 Write pseudocode or program code for the procedure displayRowAnswer(). [6] (v) The function checkWon() takes answerGrid and puzzle as parameters and compares each element in the grids. If they are identical, it returns true, otherwise returns false. 1 function checkWon(puzzle) 2 for row = 0 to 4 3 for column = 0 to 4 4 if puzzle[row, column] == answerGrid[row, column] then 5 return false 6 endif 7 next column 8 next column 9 return true 10 endfunction There are three logic errors in the function checkWon. State the line number of each error and give the corrected line. Error 1 line number Error 1 correction Error 2 line number Error 2 correction Error 3 line number Error 3 correction [3] (c)* Juan passed the two arrays as parameters, but he did consider making them globally accessible. Compare the use of global and local variables and data structures in this program. Include the use of parameters and program efficiency in your answer. [9] (d) Juan wants to create a program that will generate new Nonograms with different grid sizes. For example a Nonogram with a 10 × 10 grid or a 5 × 20 grid. Describe how the program could be written to automatically generate a new Nonogram. [4]