Question 1
State three advantages of using subroutines.
For each advantage, you must explain how the advantage is achieved.
[3 marks]
Answer
State three advantages of using subroutines.
For each advantage, you must explain how the advantage is achieved.
[3 marks]
Circular queues and linear queues are examples of data structures that can be implemented using a fixed-length array.
Explain why, when implemented using a fixed-length array, a circular queue is usually considered to be a better choice of data structure than a linear queue.
[2 marks]
Describe the steps that must be completed to remove (dequeue) an item from a circular queue that has been implemented using a fixed-length array.
[5 marks]
0 3
Describe the Halting problem.
[2 marks]
Explain the importance of the Halting problem.
[1 mark]
Figure 1 shows four sets R, S, T and U. Three dots (…) means the remaining members of the set follow the same pattern as the previous members of the set.
Figure 1
R = { a, b } S = { a, abb, abbbb, abbbbbb, …} T = { bb, bbbb, bbbbbb, …} U = { c, d, bb, b }
What is meant by the cardinality of a set?
[1 mark]
Explain what is wrong with the statement:
‘The only subsets of R are the sets { a }, { b } and { a, b }’.
[1 mark]
How many members are there in the set formed by the intersection of R and U ?
[1 mark]
The language defined by a regular expression can be represented as a set.
Explain the functionality of the | (vertical bar) metacharacter when it is used in a regular expression.
[1 mark]
The members of the set V are strings that match the regular expression a?b+
Set W is formed by the union of sets S and T.
Set X is formed by the set operation V – W.
Write a regular expression that would match with all the members of the set W.
[2 marks]
Write a regular expression that would match with all the members of the set X.
[2 marks]
(3 + 4) * 5 is an example of an infix expression. The same expression has been represented in a different expression format in Figure 2.
Figure 2
3 4 + 5 *
What is the name of the expression format used in Figure 2 ?
[1 mark]
Represent the infix expression 5 + 2 * 3 + 4 in the same expression format used in Figure 2.
[2 marks]
Figure 3 shows a graph containing five nodes. Figure 4 shows how the graph in Figure 3 could be represented using three one-dimensional arrays: Data, Dir1 and Dir2.
The graph in Figure 3 is a binary tree. A binary tree is a rooted tree where each node has at most two child nodes.
There are three properties that a graph needs to have for it to be a tree. One of those properties is that it contains no cycles.
State the other two properties of this graph that make it a tree.
[2 marks]
[7 marks]
The array Temp needs to be able to store three values when used with the binary tree shown in Figure 3, on page 6.
For some binary trees with only five nodes, the array Temp would need to be able to store five values.
Describe the structure of a five-node binary tree that would require Temp to be able to store five values.
[2 marks]
State the type of data structure the algorithm shown in Figure 5 implements using the array Temp.
[1 mark]
[1 mark]
You are advised to spend no more than 20 minutes on this section.
Enter your answers to Section B in your Electronic Answer Document.
You must save this document at regular intervals.
The question in this section asks you to write program code starting from a new program/project/file.
You are advised to save your program at regular intervals.
Write a program that gets the user to enter an integer. It should keep doing this until they enter a value greater than 0.
The program should then tell the user if they have entered a perfectly bouncy number, a bouncy number or a number that is not bouncy.
A bouncy number is a number that is not an increasing number and not a decreasing number.
An increasing number is one where each digit is greater than or equal to the previous digit in the number.
A decreasing number is one where each digit is less than or equal to the previous digit in the number.
A perfectly bouncy number is a bouncy number in which the number of digits that are followed by a larger digit is equal to the number of digits that are followed by a smaller digit.
Examples
13578 is not a bouncy number because it is an increasing number.
973 is not a bouncy number because it is a decreasing number.
98657 is a bouncy number.
1111 is not a bouncy number because it is both an increasing number and a decreasing number.
13421 is a perfectly bouncy number as exactly two digits are followed by a larger digit and there are also exactly two digits followed by a smaller digit.
1829361 is a perfectly bouncy number as exactly three digits are followed by a larger digit and there are also exactly three digits followed by a smaller digit.
13333331 is a perfectly bouncy number as there is exactly one digit followed by a larger digit and also exactly one digit followed by a smaller digit.
[12 marks]
[1 mark]
You are advised to spend no more than 20 minutes on this section.
Enter your answers to Section C in your Electronic Answer Document.
You must save this document at regular intervals.
These questions refer to the Preliminary Material and the Skeleton Program, but do not require any additional programming.
Refer either to the Preliminary Material issued with this question paper or your electronic copy.
This question is about the Puzzle class.
The DisplayPuzzle method uses concatenation.
Explain what is meant by concatenation.
[1 mark]
State the number of methods in the Puzzle class that read data from a text file.
[1 mark]
State the number of methods in the Puzzle class that read data from a binary (non-text) file.
[1 mark]
This question is about the Cell class.
Explain why IsEmpty could have been a private method instead of a public method.
[1 mark]
The CheckSymbolAllowed method in the Cell class uses a local variable.
Explain one difference between a local variable and a private class attribute.
[1 mark]
This question is about the CheckForMatchWithPattern method in the Puzzle class.
CheckForMatchWithPattern uses nested iteration and exception handling.
State the name of another method in the Puzzle class that uses nested iteration.
[1 mark]
Explain what problem would occur if the first two iteration structures in the method CheckForMatchWithPattern had been inside the exception handling structure instead of outside it.
[1 mark]
Describe what exception handling is used for in the CheckForMatchWithPattern method.
[3 marks]
A value of 10 is returned sometimes when it should not be. Figure 6 and Figure 7 show example 44 grids. The pattern in Figure 6 correctly results in a value of 10 being returned. The pattern in Figure 7 also results in a value of 10 being returned, but it should return 0.
Explain why the pattern in Figure 7 results in the value 10 being returned by the CheckForMatchWithPattern method.
[1 mark]
You are advised to spend no more than 70 minutes on this section.
Enter your answers to Section D in your Electronic Answer Document.
You must save this document at regular intervals.
These questions require you to load the Skeleton Program and to make programming changes to it.
This question refers to the method AttemptPuzzle in the Puzzle class.
The Skeleton Program is to be changed so that it checks the choice made by the user for the column number. A choice is valid if it is between one and the number of columns in the puzzle inclusive. You do not need to add checks for the row number.
The program should keep getting the user to enter a value until a valid choice has been made.
What you need to do
Task 1 Modify the method AttemptPuzzle so it checks that the value entered by the user is valid. If an invalid value is entered, the user should be made to enter another value.
Task 2 Test that the changes you have made work:
run the Skeleton Program
press the Enter key
enter 1
enter 10
enter 4
[4 marks]
[1 mark]
This question extends the Skeleton Program by tracking the user’s average score and highest score for the puzzle.
After the user chooses not to do another puzzle, the program should display the user’s highest score achieved on the puzzle and the user’s average score. Your amended Skeleton Program should calculate the average and highest scores for any number of puzzle attempts.
Example If the user:
gets a score of 30 when they complete a puzzle
decides to do another puzzle
gets a score of 20 for the second puzzle
decides not to do another puzzle
the program should display messages saying that their highest score was 30 and their average score was 25
What you need to do
Task 1 Modify the Main subroutine so that after the user decides not to attempt another puzzle their highest score for the puzzles attempted and their average score for the puzzles attempted are displayed.
Task 2 Test that the changes you have made work:
run the Skeleton Program
load the file puzzle1
enter 5
enter 5
enter X
enter Y
load the file puzzle1
enter 1
enter 4
enter X
enter N
[5 marks]
[1 mark]
This question extends the Skeleton Program by giving the user an option to shift all the cells in one row in the puzzle one place to the left.
When shifting left, the first cell in the row will move to the last position in the row.
Figure 8 shows an example puzzle and Figure 9 shows the result obtained from shifting all the cells in row 2 from the puzzle in Figure 8 one place to the left.
Figure 8
Figure 9
After the cells have been shifted, the user’s score should be reduced by 20 and the new state of the grid and the user’s new score should be displayed. No other changes to the user’s score should be made.
Shifting the cells does not count as a turn. After choosing to shift cells, the user should carry on with the rest of their turn by selecting a cell to place a symbol in.
When answering this question, you should make sure your program code will work for any size of puzzle grid.
What you need to do
Task 1 Create a new method called ShiftCellsInRowLeft in the Puzzle class that takes an integer parameter that specifies the row number to use with the shift.
Each cell, in the row indicated by the parameter, should be moved in Grid so that it is one place to the left; the leftmost cell should shift to the end of the row.
Task 2 Modify the AttemptPuzzle method in the Puzzle class so that it gives the user the option to shift the cells in a row. Inside the iteration structure used to get a row number from the user your program should:
display a modified message telling the user to enter a row number or to enter 0 to shift cells
if the user enters 0: o ask the user to enter the row number for the row to shift o call the method ShiftCellsInRowLeft with the number entered as a parameter o subtract 20 from the user’s score o display the new grid o display the new score o set Valid to be false.
Task 3 Test that the changes you have made work:
run the Skeleton Program
load the file puzzle2
enter 0
enter 1
enter 1
[11 marks]
[1 mark]
The movement of the cells within a row can be described using vectors.
State the 2-vector that describes the movement for all but the leftmost cell in the row.
[1 mark]
State the 2-vector that describes the movement for the leftmost cell in the row.
Your vector should work for any grid size.
[1 mark]
This question extends the Skeleton Program so that there is a new type of cell – a countdown cell.
A countdown cell has a timer associated with it that decreases by one each time the user tries to place a symbol in the puzzle grid.
The location of a countdown cell is shown by a numeric digit representing the current value of its timer. When its timer reaches zero, the cell’s symbol changes to an @.
A countdown cell should be added to the board each time the user’s score is increased. The location for the countdown cell should be an empty cell on the grid and selected randomly.
What you need to do
Task 1 Create a new class called CountdownCell that is a subclass of the BlockedCell class. Create a constructor for the CountdownCell class that sets the initial value of the timer and makes sure that the symbol displayed for the cell will be the value of its timer. The first symbol that should be displayed for the timer value of a CountdownCell is 3 Create a method UpdateCell in the CountdownCell class that:
overrides the method from the base class
decreases the value of the timer by one
changes the symbol to @ when the timer has a value of zero.
Task 2 Modify the AttemptPuzzle method in the Puzzle class so that, if the amount to add to the score is greater than zero, it selects an empty cell in the grid at random and replaces that cell with a new CountdownCell.
Task 3 Modify the AttemptPuzzle method in the Puzzle class so that, immediately before it checks if the number of symbols left is zero, it calls the UpdateCell method for each cell in the grid.
Task 4 Test that the changes you have made work:
run the Skeleton Program
load the file puzzle3
enter 1
enter 4
enter X
place a T in an empty cell
place a T in an empty cell
place a T in an empty cell.
[13 marks]
[1 mark]
The paper as printed
Spotted a mistake on this page?Report a problem