Data types, arrays and files – A Level Computer Science (old syllabus, to 2021) (9608)

118 questions · page 8 of 8

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
106
9608/42/M/J/17 · Question 3May/June 2017Paper 4230 marks
Loading question…
Question text

3
A programmer is writing a treasure island game to be played on the computer. The island is
a rectangular grid, 30 squares by 10 squares. Each square of the island is represented by an
element in a 2D array. The top left square of the island is represented by the array element [0, 0].
There are 30 squares across and 10 squares down.
The computer will:
•
generate three random locations where treasure will be buried
•
prompt the player for the location of one square where the player chooses to dig
•
display the contents of the array by outputting for each square:
–
'.' for only sand in this square
–
'T' for treasure still hidden in sand
–
'X' for a hole dug where treasure was found
–
'O' for a hole dug where no treasure was found.
Here is an example display after the player has chosen to dig at location [9, 3]:
…
…
…
…
…
… T …
…
…
… T …
...X …
The game is to be implemented using object-oriented programming.
The programmer has designed the class IslandClass. The identifier table for this class is:
Identifier
Data type
Description
2D array to represent the
Grid
ARRAY[0 : 9, 0 : 29] OF CHAR
squares of the island
instantiates an object of class
IslandClass and initialises
Constructor()
all squares to sand
generates a pair of random
numbers used as the grid
HideTreasure()
location of treasure and
marks the square with 'T'
takes as parameters a valid
grid location and marks the
DigHole(Row, Column)
square with 'X' or 'O' as
appropriate
takes as parameter a valid
grid location and returns the
grid value for that square
GetSquare(Row, Column) CHAR
from the IslandClass
object
(a) The programmer designed the pseudocode for the main program as follows:
DECLARE Island : IslandClass.Constructor()
// instantiate object
CALL DisplayGrid()
// output island squares
FOR Treasure ← 1 TO 3
// hide 3 treasures
CALL Island.HideTreasure()
ENDFOR
CALL StartDig()
// user to input location of dig
CALL DisplayGrid()
// output island squares
Write program code to implement this pseudocode.
Programming language used …
Program code …
…
…
…
…
…
…
…
…
…
…

107
9608/21/O/N/16 · Question 4Oct/Nov 2016Paper 2122 marks
Loading question…
Question text

4
A company employs Ahmed as a programmer.
(a) At College, before joining the company, Ahmed used two items of software for programming:
•
a text editor
•
a compiler
Describe how he could have developed programs using these software tools.
Include in the description the terms ‘object code’ and ‘source code’.
…
…
…
…
…
…
… [3]
(b) Ahmed now uses an Integrated Development Environment (IDE) for programming.
(i)
State one feature an IDE provides to help with the identification of syntax errors.
…
… [1]
(ii)
State one feature an IDE provides to carry out white box testing.
…
… [1]
(c) The company maintains a file of product data. Ahmed is to write a program to add a new
product and search for a product based on the structure diagram shown:
Product
processing
Add
Search for
product
product
By
By
By
product code
description
price
The program records the following data for each product:
•
product code
•
product description
•
product retail price
The text file PRODUCTS stores each data item on a separate line, as shown below:
File PRODUCTS
0198
Plums(10kg)
11.50
0202
Onions(20kg)
10.00
0376
Mango chutney(1kg)
02.99
0014
Mango(10kg)
12.75
The program uses the variables shown in the identifier table.
Identifier
Data type
Description
Storing the code, description and
PRODUCTS
TEXT FILE
retail price for all current products
PCode
ARRAY[1:1000] OF STRING
Array storing the product codes
PDescription
ARRAY[1:1000] OF STRING
Array storing the product descriptions
PRetailPrice
ARRAY[1:1000] OF REAL
Array storing the product retail prices
i
INTEGER
Array index used by all three arrays
(i) The first operation of the program is to read all the product data held in file PRODUCTS
and write them into the three 1D arrays.
Complete the pseudocode below.
OPEN …
1
i
WHILE …
READFILE ("PRODUCTS", … )
READFILE ("PRODUCTS", … )
READFILE ("PRODUCTS", … )
…

108
9608/42/O/N/16 · Question 3Oct/Nov 2016Paper 4216 marks
Loading question…
Question text

3
(a) The numerical difference between the ASCII code of an upper case letter and the ASCII code
of its lower case equivalent is 32 denary (32 10 ).
For example, 'F' has ASCII code 70 and 'f' has ASCII code 102.
Bit number
7
6
5
4
3
2
1
0
ASCII code
ASCII code in binary
0
1
0
0
0
1
1
0
70
0
1
1
0
0
1
1
0
102
The bit patterns differ only at bit number 5. This bit is 1 if the letter is lower case and 0 if the
letter is upper case.
(i)
A program needs a mask to ensure that a letter is in upper case.
Write the binary pattern of the mask in the space provided in the table below.
Bit number
7
6
5
4
3
2
1
0
ASCII code
ASCII code in binary
0
1
0
0
0
1
1
0
70
0
1
1
0
0
1
1
0
102
Mask
Give the bit-wise operation that needs to be performed using the mask and the
ASCII code.
… [2]
(ii)
A program needs a mask to ensure that a letter is in lower case.
Write the binary pattern of the mask in the space provided in the table below.
Bit number
7
6
5
4
3
2
1
0
ASCII code
ASCII code in binary
0
1
0
0
0
1
1
0
70
0
1
1
0
0
1
1
0
102
Mask
Give the bit-wise operation that needs to be performed using the mask and the
ASCII code.
… [2]
The following table shows part of the instruction set for a processor which has one general
purpose register, the Accumulator (ACC), and an index register (IX).
Instruction
Explanation
Op
Operand
code
LDM
#n
Immediate addressing. Load the number n to ACC.
Direct addressing. Load the contents of the given address to
LDD
<address>
ACC.
Indexed addressing. Form the address from <address> +
LDX
<address>
the contents of the index register. Copy the contents of this
calculated address to ACC.
LDR
#n
Immediate addressing. Load the number n into IX.
STO
<address>
Store the contents of ACC at the given address.
INC
<register>
Add 1 to the contents of the register (ACC or IX).
CMP
<address>
Compare the contents of ACC with the contents of <address>.
CMP
#n
Compare the contents of ACC with number n.
Following a compare instruction, jump to <address> if the
JPE
<address>
compare was True.
Following a compare instruction, jump to <address> if the
JPN
<address>
compare was False.
Bitwise AND operation of the contents of ACC with the
AND
#n
operand.
Bitwise AND operation of the contents of ACC with the
AND
<address>
contents of <address>.
Bitwise XOR operation of the contents of ACC with the
XOR
#n
operand.
Bitwise XOR operation of the contents of ACC with the
XOR
<address>
contents of <address>.
OR
#n
Bitwise OR operation of the contents of ACC with the operand.
Bitwise OR operation of the contents of ACC with the contents
OR
<address>
of <address>.
Output to the screen the character whose ASCII value is
OUT
stored in ACC.
END
Return control to the operating system.
A programmer is writing a program that will output the first character of a string in upper case and
the remaining characters of the string in lower case.
The program will use locations from address WORD onwards to store the characters in the string.
The location with address LENGTH stores the number of characters that make up the string.
The programmer has started to write the program in the following table. The comment column
contains descriptions for the missing program instructions.
(b) Complete the program using op codes from the given instruction set.
Op
Label
Operand
Comment
code
START:
// initialise index register to zero
// get first character of WORD
// ensure it is in upper case using MASK1
// output character to screen
// increment index register
// load 1 into ACC
// store in COUNT
LOOP:
// load next character from indexed address WORD
// make lower case using MASK2
// output character to screen
// increment COUNT starts here
// is COUNT = LENGTH ?
// if FALSE, jump to LOOP
// end of program
COUNT:

Advertisement
109
9608/42/O/N/16 · Question 4Oct/Nov 2016Paper 4214 marks
Loading question…
Question text

4
Circle the programming language that you have studied:
Visual Basic (console mode)
Python
Pascal
Delphi (console mode)
(a) (i)
Name the programming environment you have used when typing in program code.
…
…
List three features of the editor that helped you to write program code.
1 …
…
2 …
…
3 …
… [3]
(ii)
Explain when and how your programming environment reports a syntax error.
When …
…
…
How …
…
… [2]
The table shows a module definition for BubbleSort in three programming languages.
(iii)
Study one of the examples. Indicate your choice by circling A, B or C:
A
B
C
A) Python
01
def BubbleSort(SList, Max):
02
NoMoreSwaps = False
03
while NoMoreSwaps == False:
04
NoMoreSwaps = True
05
for i in (Max - 1):
06
if SList[i] > SList[i + 1]:
07
NoMoreSwaps = True
08
Temp = SList[i]
09
SList[i] = SList[i + 1]
10
SList[i + 1] = Temp
B) Pascal/Delphi
01
PROCEDURE BubbleSort(VAR SList : ARRAY OF INTEGER; Max : INTEGER);
02
VAR NoMoreSwaps : BOOLEAN; i, Temp : INTEGER;
03
BEGIN
04
REPEAT
05
NoMoreSwaps := TRUE;
06
FOR i := 1 TO (Max – 1)
07
IF SList[i] > SList[i + 1]
08
THEN
09
BEGIN
10
NoMoreSwaps := TRUE;
11
Temp := SList[i];
12
SList[i] := SList[i + 1];
13
SList[i + 1] := Temp;
14
END;
15
UNTIL NoMoreSwaps;
16
END;
C) Visual Basic
01
Sub BubbleSort(ByRef SList() As Integer, ByVal Max As Integer)
02
Dim NoMoreSwaps As Boolean, i, Temp As Integer
03
Do
04
NoMoreSwaps = True
05
For i : 0 To (Max – 1)
06
If SList(i) > SList(i + 1) Then
07
NoMoreSwaps = True
08
Temp = SList(i)
09
SList(i) = SList(i + 1)
10
SList(i + 1) = Temp
11
End If
12
Next
13
Loop Until (NoMoreSwaps = True)
14
End Sub
The programming environment reported a syntax error in the BubbleSort code.
State the line number …
Write the correct code for this line.
… [2]
(b) (i)
State whether programs written in your programming language are compiled or
interpreted.

110
9608/21/M/J/16 · Question 3May/June 2016Paper 2118 marks
Loading question…
Question text

3
A string encryption function is needed. The encryption uses a simple character-substitution method.
In this method, a new character substitutes for each character in the original string. This will create
the encrypted string.
The substitution uses the 7-bit ASCII value for each character. This value is used as an index for a
1D array, Lookup, which contains the substitute characters.
Lookup contains an entry for each of the ASCII characters. It may be assumed that the original
string and the substitute characters are all printable.
For example:
•
‘A’ has ASCII value 65
•
Array element with index 65 contains the character ‘Y’ (the substitute character)
•
Therefore, ‘Y’ substitutes for ‘A’
•
There is a different substitute character for every ASCII value
The programmer writes a function, EncryptString, to return the encrypted string. This function
will receive two parameters, the original, PlainText string and the 1D array.
(a) The first attempt at writing the pseudocode for this function is shown below.
Complete the pseudocode.
For the built-in functions list, refer to the Appendix on page 18.
FUNCTION EncryptString( … ) RETURNS STRING
DECLARE … , … : CHAR
DECLARE OldCharValue : …
DECLARE n : INTEGER
DECLARE OutString : STRING
… //initialise the return string
//loop through PlainText to produce OutString
FOR n
1 TO … //from first to last character
… //get next character
OldChar
… //find the ASCII value
OldCharValue
… //look up substitute character
NewChar
… //concatenate to OutString
ENDFOR
…
ENDFUNCTION
[10]
(b) Additional code needs to be written to allow the user to change some of the characters in the
array Lookup.
The user will input:
•
the array start position
•
the number of elements to change
•
each new substitute character
At the end, the program will finally output a confirmation message.
The first version of the algorithm is represented by the flowchart on the following page.
Write program code to declare the array Lookup.
(i)
Programming language …
…
…
… [2]
(ii)
Write program code to implement the flowchart design.
In addition to the Lookup array, assume that the following variables have been declared:
StartPos, NumToChange, n, NewChar
Programming language …
…
…
…
…
…
…
…

111
9608/21/M/J/16 · Question 5May/June 2016Paper 2113 marks
Loading question…
Question text

5
Toni has a large collection of jazz CDs that are stored in different places. She wants to record
where the CDs are stored. She decides to write a program to do this.
The program must store the data in a file, MyMusic.
(a) (i)
Why is a file needed?
…
… [1]
MyMusic is a text file with the data for each CD as one line of text.
(ii)
Data for a typical CD are:
Title:
Kind of Green
Artist:
Miles Coltrane
Location:
Rack1-5
The line will be formed by concatenating the three data items.
For the example above, the line stored will be:
Kind of GreenMiles ColtraneRack1-5
Describe a problem that might occur when organising the data in this way.
…
…
…
…
Describe a possible solution.
…
…
…
…
[4]
(b) Toni must input the data into the file for all of her CDs.
A procedure, InputData, is needed to do this.
Toni designs the procedure and chooses the following identifiers:
Identifier
Data type
CDTitle
STRING
CDArtist
STRING
CDLocation
STRING
The procedure repeatedly performs the following steps:
•
input a CD title (A rogue value of “##” is to be used to end the input)
•
input the artist
•
input the location
•
create the text line
•
write the text line to the file
When the rogue value is encountered the file is closed.
Write program code for the procedure InputData.
Visual Basic and Pascal: You should include declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.
Programming language …
…
…
…
…
…
…
…
…

112
9608/23/M/J/16 · Question 2May/June 2016Paper 2311 marks
Loading question…
Question text

2
The engine management system of a car includes an energy-saving facility. When certain
conditions are met, this facility will automatically stop the engine.
The system is to be software-based. It will include a subroutine, EnergySaver, which repeatedly
takes data from sensors in the car. The subroutine decides whether or not to set the EngineStop
value.
The table of identifiers used by this subroutine is shown below.
(a) Complete the identifier table below by stating the data types.
Identifier
Data type
Description
Accelerator pedal position
Values: 0 to 100 in steps of 1
Accelerator
…
Meaning:
0: none (not pressed)
100: maximum (fully pressed)
Engine temperature in °C
EngineTemp
…
(-50 to +150 stored to 1 decimal place)
Normal engine temperature in °C
NormalTemp
…
Whole number; typical value 90
Road speed of car (in km/hr)
Speed
…
Values: 0 to 200 in steps of 1
Value used to signal engine must be stopped
Possible values:
EngineStop
…
TRUE: stop engine
FALSE: run engine
[5]
The condition for stopping the engine is that all three of the following are true:
•
Accelerator is not pressed
•
Engine temperature is normal or above
•
Car speed is zero
The initial design stage will produce a prototype of EnergySaver, with a user interface.
The structured English for this is:
1.
INPUT value for accelerator pedal position
2.
INPUT value for engine temperature
3.
INPUT value for normal engine temperature
4.
INPUT value for car speed
5.
EVALUATE engine stopping condition
6.
IF stopping condition satisfied SET engine stop value to TRUE
7.
IF stopping condition not satisfied SET engine stop value to FALSE
8.
OUTPUT message indicating engine stop value
(b) Write the pseudocode equivalent of the structured English. Use the identifiers from the table
in part (a).
For the built-in functions list, refer to the Appendix on the last page.
…
…
…
…
…
…
…
…
…
…
…
…
…
…

113
9608/23/M/J/16 · Question 3May/June 2016Paper 2317 marks
Loading question…
Question text

3
String encryption was implemented using a simple character-substitution method. A function,
Decrypt, is needed to reverse the encryption process and return the original character.
The encryption uses the 7-bit ASCII value for each character. This value is used as an index for a
1D array, Lookup, which contains the substitute characters.
Lookup contains an entry for each of the ASCII characters.
This function, Decrypt, will accept two parameters, a single character, CipherChar, and the 1D
array, Lookup.
The steps involved in Decrypt are follows:
•
Search for the character in the array
•
Note the index value where the character is found (the index value is the ASCII value of the
original character)
•
Use the index value to obtain the original character
(a) The first attempt at writing the pseudocode for this function is shown below.
Complete the pseudocode.
For the built-in functions list, refer to the Appendix on the last page.
FUNCTION Decrypt( … , … ) RETURNS CHAR
DECLARE Found
: …
DECLARE … : INTEGER
DECLARE OriginalChar
: CHAR
1
// …
Index
FALSE
Found
//search for CipherChar in Lookup:
WHILE …
//compare CipherChar with this array element:
IF …
THEN
… //Set the flag
ELSE
Index … //Move to next array element
ENDIF
ENDWHILE
//dropped out of loop so must have found CipherChar:
… //convert Index to original character
RETURN …
ENDFUNCTION
[11]
Question 3 continues on page 8.
(b) A program is to be written to output part of the Lookup array.
The design of the algorithm is shown below.
It may be assumed that the characters output from Lookup are all printable.
START
Input StartIndex and
NumberToOutput
Index
StartIndex
Loop
Produce OriginalChar from
Index (using built-in function)
Assign current array element to
Increment Index
CipherChar
Output message line
(see examples on the next page)
NO
End of
loop?
YES
STOP
For example, for the input of 65 and 3, the output will be:
Index 65: Character A has substitute character Y
Index 66: Character B has substitute character Q
Index 67: Character C has substitute character F
Write program code to implement the flowchart design.
In addition to the Lookup array, assume that the following variables have been declared:
StartIndex, NumberToOutput, Index
Programming language …
…
…
…
…
…

Advertisement
114
9608/23/M/J/16 · Question 5May/June 2016Paper 2316 marks
Loading question…
Question text

5
Claudia stores her large collection of music CDs in different places. Claudia wants to record where
she stores each CD. She decides to write a program to do this.
Data items for a typical CD are:
Title:
Kind of Green
Artist:
Miles Coltrane
Location: Rack3-23
The data is to be stored in a text file, MyMusic. Each line of the text file will be a string, formed by
concatenating the three data items.
Before concatenation, the title and artist will each be made into a fixed-length string of 40
characters. Space characters may need to be added to each data item.
The location is always 8 characters long.
(a) (i)
Explain the benefit of making the stored data into fixed-length strings.
…
…
…
…
State a drawback of this file design.
…
…
[3]
When Claudia buys a new CD, the CD data must be added to the existing file, MyMusic.
(ii)
She has written a procedure in pseudocode. This has the following file-handling
statements:
OPENFILE "MyMusic" FOR WRITE
WRITEFILE "MyMusic", OutputString
CLOSEFILE "MyMusic"
There is a problem with the logic of this pseudocode.
State the problem.
…
…
Identify the effect it will have if the final code is implemented in this way.
…
…
Give a possible solution.
…
…
[3]
(b) Claudia needs to output a list of all the CDs in a particular location.
She designs a procedure, OutputLocationList, to do this. She also chooses the following
identifiers:
Identifier
Data type
CDTitle
STRING
CDArtist
STRING
CDLocation
STRING
The procedure will:
•
prompt for the name of the location
•
input the location (such as “Rack3-23”)
•
search the file for all CDs at this location
•
output the title and artist of each CD found
•
output the total number of CDs found at that location (such as “17 CDs found”)
Write program code for the procedure OutputLocationList.
Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.
Programming language …
…
…

115
9608/41/M/J/16 · Question 1May/June 2016Paper 4119 marks
Loading question…
Question text

1
A linked list abstract data type (ADT) is to be used to store and organise surnames.
This will be implemented with a 1D array and a start pointer. Elements of the array consist of a
user-defined type. The user-defined type consists of a data value and a link pointer.
Identifier
Data type
Description
LinkedList
RECORD
User-defined type
Surname
STRING
Surname string
Ptr
INTEGER
Link pointers for the linked list
Write pseudocode to declare the type LinkedList.
(a) (i)
…
…
…
… [3]
The 1D array is implemented with an array SurnameList of type LinkedList.
(ii)
Write the pseudocode declaration statement for SurnameList. The lower and upper
bounds of the array are 1 and 5000 respectively.
… [2]
(b) The following surnames are organised as a linked list with a start pointer StartPtr.
StartPtr: 3
1
2
3
4
5
6
5000
Liu
Yang
Chan
Wu
Zhao
Huang
Surname
…
4
5
6
2
0
1
Ptr
…
State the value of the following:
SurnameList[4].Surname … [1]
(i)
SurnameList[StartPtr].Ptr … [1]
(ii)
(c) Pseudocode is to be written to search the linked list for a surname input by the user.
Identifier
Data type
Description
ThisSurname
STRING
The surname to search for
Current
INTEGER
Index to array SurnameList
Index to array SurnameList. Points to the
StartPtr
INTEGER
element at the start of the linked list
(i)
Study the pseudocode in part (c)(ii).
Complete the table above by adding the missing identifier details.
[2]
(ii)
Complete the pseudocode.
01 Current ← …
02 IF Current = 0
03
THEN
04
OUTPUT …
05
ELSE
06
IsFound ← …
07
INPUT ThisSurname
08
REPEAT
09
IF … = ThisSurname
10
THEN
11
IsFound ← TRUE
12
OUTPUT "Surname found at position ", Current
13
ELSE
14
// move to the next list item
15
…
16
ENDIF
17
UNTIL IsFound = TRUE OR …
18
IF IsFound = FALSE
19
THEN
20
OUTPUT "Not Found"
21
ENDIF
22 ENDIF
[6]

116
9608/41/M/J/16 · Question 2May/June 2016Paper 417 marks
Loading question…
Question text

2
(a) (i)
State what is meant by a recursively defined procedure.
…
… [1]
(ii)
Write the line number from the pseudocode shown in part (b) that shows the
procedure X is recursive. …
[1]
(b) The recursive procedure X is defined as follows:
01 PROCEDURE X(Index, Item)
02
IF MyList[Index] > 0
03
THEN
04
IF MyList(Index) >= Item
05
THEN
06
MyList[Index] ← MyList[Index + 1]
07
ENDIF
08
CALL X(Index + 1, Item)
09
ENDIF
10 ENDPROCEDURE
An array MyList is used to store a sorted data set of non-zero integers. Unused cells contain
zero.
1
2
3
4
5
6
7
8
9
10
MyList
3
5
8
9
13
16
27
0
0
0
(i)
Complete the trace table for the dry-run of the pseudocode for the procedure
CALL X(1, 9).
MyList
Index
Item
1
2
3
4
5
6
7
8
9
10
3
5
8
9
13
16
27
0
0
0
1
9
[4]
State the purpose of procedure X when used with the array MyList.
(ii)
…
… [1]

117
9608/41/M/J/16 · Question 5May/June 2016Paper 4114 marks
Loading question…
Question text

5
(a) A program calculates the exam grade awarded from a mark input by the user. The code is
written as a function CalculateGrade.
The function:
has a single parameter Mark of INTEGER data type
•
returns the grade awarded Grade of STRING data type
•
The logic for calculating the grade is as follows:
Mark
Grade
Under 40
FAIL
40 and over and under 55
PASS
55 and over and under 70
MERIT
70 and over
DISTINCTION
The programmer designs the following table for test data:
Mark
Description
Expected result (Grade)
Normal
Abnormal
Extreme/Boundary
(i)
Complete the table above.
[3]
(ii)
State why this table design is suitable for black box testing.
…
… [1]
(b) When designing and writing program code, explain what is meant by:
•
an exception
•
exception handling
…
…
…
…
…
… [3]
(c) A program is to be written to read a list of exam marks from an existing text file into a 1D array.
Each line of the file stores the mark for one student.
State three exceptions that a programmer should anticipate for this program.
1 …
…
2 …
…
3 …
… [3]
(d) The following pseudocode is to read two numbers:
01 DECLARE Num1
: INTEGER
02 DECLARE Num2
: INTEGER
03 DECLARE Answer : INTEGER
04 TRY
05
OUTPUT "First number..."
06
INPUT Num1
07
OUTPUT "Second number..."
08
INPUT Num2
09
Answer ← Num1 / (Num2 – 6)
10
OUTPUT Answer
11 EXCEPT ThisException : EXCEPTION
12
OUTPUT ThisException.Message
13 FINALLY
14
// remainder of the program follows
…
29
30 ENDTRY
The programmer writes the corresponding program code.
A user inputs the number 53 followed by 6. The following output is produced:
First number...53
Second number...6
Arithmetic operation resulted in an overflow
(i)
State the pseudocode line number which causes the exception to be raised.
…
[1]
Explain the purpose of the pseudocode on lines 11 and 12.
(ii)
…
…

118
9608/41/M/J/16 · Question 6May/June 2016Paper 4117 marks
Loading question…
Question text

6
In a board game, one player has white pieces and the other player has black pieces. Players take
alternate turns to move one of their pieces. White always makes the first move.
The game ends if:
•
a player is unable to make a move when it is their turn. In this case, there is no winner. This is
called ‘stalemate’.
•
a player wins the game as a result of their last move and is called a ‘winner’.
(a) A state-transition diagram is drawn to clarify how the game is played.
Complete the following state-transition diagram.
WHITE
WINS
No move
possible
:+,7(·6
TURN
Stalemate
BLACK moves
BLACK
%/$&.·6
WINS
TURN
Winning
move
[4]
(b) The layout of the board at the start of the game is shown below:
x
y
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
The programmer decides to use a 2D array to represent the board. The index numbering to
be used is as shown.
Each square on the board is either occupied by one piece only, or is empty.
The data stored in the array indicate whether or not that square is occupied, and if so, with a
black piece or a white piece.
(i)
Write program code to initialise the contents of the array to represent the board at the
start of the game. Use characters as follows for each square:
'B' represents a black piece
'W' represents a white piece
'E' represents an empty square
Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.
Programming language …
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

Advertisement