Data types, arrays and files – A Level Computer Science (old syllabus, to 2021) (9608)
24 questions · page 1 of 2 · Clear filters
Question text
6
A theatre company stores customer login details to allow customers to book tickets online.
A hash table stores login details for 2000 customers.
Each customer’s details are stored in a record.
The declaration for CustomerRecord is:
TYPE CustomerRecord
DECLARE UserID : STRING
DECLARE PINNumber : INTEGER
ENDTYPE
A 1D array, CustomerDetails, is used to implement the hash table. CustomerDetails is a
global array. The 1D array has 6000 elements.
(a) The procedure InitialiseHashTable() initialises the hash table. UserID is initialised as
an empty string, and PINNumber initialised to 0 for all of the records.
Write pseudocode for the procedure InitialiseHashTable().
…
…
…
…
…
…
…
… [4]
(b) The function InsertRecord() is used to insert a new record into the hash table.
The function, Hash():
•
takes a UserID as a parameter
•
performs the hashing algorithm
•
returns the calculated index of the user ID within the hash table.
If the hash table is full, the function InsertRecord() returns -1. If there is space available
in the hash table, the record is inserted, and it returns the position of this record in the array.
Complete the pseudocode for the function.
FUNCTION InsertRecord(NewRecord : CustomerRecord) RETURNS INTEGER
DECLARE Count : INTEGER
DECLARE Index : INTEGER
0
Count
Hash( … )
Index
WHILE (CustomerDetails[Index].UserID <> "") … (Count <= 5999)
Index + 1
Index
Count + 1
Count
IF Index > 5999
THEN
…
ENDIF
ENDWHILE
IF Count > 5999
THEN
…
ELSE
…
CustomerDetails[ … ]
…
ENDIF
ENDFUNCTION
[7]
Question text
7
(a) A shirt design company has an order form to order shirts. Customers can order multiple shirts
using the same form.
The customer details section has the data:
•
name
•
address
•
telephone number.
The order details section has the data:
•
shirt ID
•
colour
•
cost.
A total cost for the order is also calculated.
The cost of each shirt is dependent on the size ordered. The sizes customers can order are
small, medium and large.
Complete the following JSP data structure diagram for the order form.
Order Form
Customer Details
Total Cost
[7]
(b) Each customer’s order is stored as a record in a file. The customers’ orders are stored in the
order in which they arrive in the file and no key field is used.
(i)
Identify this type of file structure.
… [1]
(ii)
Identify two other types of file structure.
1 …
2 …
[2]
(c) The procedure UpdateTelephone() allows the shirt company to update the record for a
customer’s details. The procedure will update the telephone number.
The program stores customer details as a custom data type, Customer.
The definition for this data type is:
TYPE Customer
Name : STRING
Address : STRING
TelephoneNumber : STRING
ENDTYPE
The procedure UpdateTelephone() takes the customer record to be updated and the new
telephone number as parameters. It then updates the telephone number in the record.
Complete the pseudocode for the procedure UpdateTelephone().
PROCEDURE UpdateTelephone( … ThisCustomer : Customer,
… NewTelephoneNumber : STRING)
…
ENDPROCEDURE
[3]
(d) The shirt company is looking to implement a system to reward customers. The system
includes:
•
10% discount on orders over $50
•
free gift if order over $50 and if the order is placed on a Monday
•
additional 5% discount if a customer has a loyalty card
•
free delivery for a customer with a loyalty card and spends over $50.
Complete the following decision table for this system.
Order over $50
Y
Y
Y
Y
N
N
N
N
Conditions
Monday
Y
Y
N
N
Y
Y
N
N
Loyalty card
Y
N
Y
N
Y
N
Y
N
Additional 5% discount
10% discount
Actions
Free gift
Free delivery
[4]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of the University of
Question text
2
A business is developing a program that stores each customer’s username and password in a
hash table.
The hash table will be implemented as a 1D array, CustomerLogIn, of the custom data type
CustomerRecord.
The declaration for CustomerRecord is:
TYPE CustomerRecord
DECLARE Username : STRING
DECLARE Password : STRING
ENDTYPE
The hash table will store a maximum of 3000 records. The key field will be Username.
(a) The program declares the hash table and initialises the username and password of all the
records to an empty string.
Write pseudocode to declare and initialise the hash table.
…
…
…
…
…
…
…
… [4]
(b) (i)
A function, SearchHashTable() will search for a customer’s record in the hash table.
The function Hash():
•
takes a Username as a parameter
•
performs the hashing algorithm
•
returns the calculated index of the username within the hash table.
The function SearchHashTable():
•
takes the username to search for as a parameter
•
uses Hash() to calculate the first index of this username within the hash table
•
returns either the index of the username if found, or –1 if not found.
Complete the pseudocode for the function SearchHashTable().
FUNCTION SearchHashTable(BYVALUE SearchUser : STRING) RETURNS …
DECLARE Index : INTEGER
DECLARE Count : INTEGER
… (SearchUser)
Index
Count
0
WHILE (CustomerLogIn[Index] … <> … )
AND(CustomerLogIn[Index].Username <> "")
AND(Count < 2999)
Index
Index + 1
Count
Count + 1
IF Index > …
THEN
Index
0
ENDIF
ENDWHILE
IF CustomerLogIn[Index].Username = …
THEN
RETURN Index
ELSE
RETURN …
ENDIF
ENDFUNCTION
[7]
(ii)
Explain the purpose of the variable Count in the function SearchHashTable().
…
…
…
… [2]
Question text
3
Recursive algorithms can be used when creating programs.
(a) Describe what is meant by a recursive algorithm.
…
…
…
…
…
… [3]
(b) A string that is a palindrome, reads the same forwards as it does backwards. For example,
the name Anna is a palindrome.
The function Substring(Variable, StartingCharacter, NumberOfCharacters)
returns one or more characters from a string. The first character is at position 0.
For example, the string "Happy" is stored in the variable Word.
•
Substring(Word, 1, 1) would return the character "a".
•
Substring(Word, 2, 3) would return the characters "ppy".
The function Length() returns the length of the string as an integer. For example,
Length(Word) returns 5.
The following is a recursive function to find out whether a string is a palindrome. The function
returns True if the parameter is a palindrome, and returns False if it is not a palindrome.
Complete the pseudocode for the recursive algorithm to indicate whether a string is a
palindrome.
FUNCTION IsPalindrome(CheckWord : STRING) RETURNS BOOLEAN
IF … <= 1
THEN
RETURN …
ENDIF
IF Substring(CheckWord, 0, 1) <>
Substring(CheckWord, … (CheckWord)-1, 1)
THEN
RETURN …
ELSE
RETURN … ( Substring(CheckWord, 1,
Length(CheckWord)-2))
ENDIF
ENDFUNCTION
[5]
(c) The function FindPower() is a recursive function that calculates the result of a base number
to the power of the exponent. For example, the result of 2 4 = 16, as 2*2*2*2 = 16. In this
example, 2 is the base number and 4 is the exponent.
The base number and the exponent are passed as parameters.
Write pseudocode for the recursive function FindPower(). Assume both the base and the
exponent are positive integers.
…
…
…
…
…
…
…
…
…
Question text
8
Files can be structured in serial, sequential or random format.
Tick (3) one box in each row to show whether the statement applies to Serial, Sequential or
Random format.
Statement
Serial
Sequential
Random
Uses a hashing algorithm
No key field is used when storing data, for example,
it is stored in chronological order
Collisions can occur
Least efficient for a very large number of records
Most efficient for a very large number of records
[3]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of the University of
Question text
1
(a) A stack contains the values 'red', 'blue', 'green' and 'yellow'.
Top of stack
yellow
green
blue
red
(i)
Show the contents of the stack in part(a) after the following operations.
POP()
PUSH('purple')
PUSH('orange')
[1]
(ii)
Show the contents of the stack from part(a)(i) after these further operations.
POP()
POP()
PUSH('brown')
POP()
PUSH('black')
[1]
(b) A queue is an alternative Abstract Data Type (ADT).
Describe a queue.
…
…
…
…
…
… [3]
Question text
2
A computer games club wants to run a competition. The club needs a system to store the scores
achieved in the competition.
A selection of score data is as follows:
99, 125, 121, 97, 109, 95, 135, 149
(a) A linked list of nodes will be used to store the data. Each node consists of the data, a left
pointer and a right pointer. The linked list will be organised as a binary tree.
(i)
Complete the binary tree to show how the score data above will be organised.
RootPointer
The symbol ∅ represents a null pointer.
LeftPointer
RightPointer
99
97
125
∅
121
[5]
(ii)
The following diagram shows a 2D array that stores the nodes of the binary tree’s linked
list.
Add the correct pointer values to complete the diagram, using your answer from
part (a)(i).
RootPointer
Index LeftPointer
Data
RightPointer
0
99
0
1
125
2
121
3
97
FreePointer
4
109
5
95
6
135
7
149
8
[6]
(b) The club also considers storing the data in the order in which it receives the scores as a
linked list in a 1D array of records.
The following pseudocode algorithm searches for an element in the linked list.
Complete the six missing sections in the algorithm.
FUNCTION FindElement(Item : INTEGER) RETURNS …
… ← RootPointer
WHILE CurrentPointer … NullPointer
IF List[CurrentPointer].Data <> …
THEN
CurrentPointer ← List[ … ].Pointer
ELSE
RETURN CurrentPointer
ENDIF
ENDWHILE
CurrentPointer ← NullPointer
… CurrentPointer
ENDFUNCTION
[6]
(c) The games club is looking at two programming paradigms: imperative and object-oriented
programming paradigms.
Describe what is meant by the imperative programming paradigm and the object-oriented
programming paradigm.
(i)
Imperative …
…
…
…
…
… [3]
(ii)
Object-oriented …
…
…
…
…
… [3]
(d) Players complete one game to place them into a category for the competition. The games club
wants to implement a program to place players into the correct category. The programmer
has decided to use object-oriented programming (OOP).
The highest score that can be achieved in the game is 150. Any score less than 50 will not
qualify for the competition. Players will be placed in a category based on their score.
The following diagram shows the design for the class Player. This includes the properties
and methods.
Player
Sco
Question text
1
A company wants an online marking system for an examination.
(a) The following is a selection of data showing final marks.
36, 45, 21, 65, 66, 13, 54, 53, 34
A linked list of nodes will be used to store the data. Each node consists of the data, a left
pointer and a right pointer. The linked list will be organised as a binary tree.
(i)
Complete the binary tree to show how the data above will be organised.
RootPointer
The symbol Ø represents a
null pointer
LeftPointer
RightPointer
36
45
21
Ø
65
66
Ø
Ø
[5]
(ii)
The following diagram shows a 2D array that stores the nodes of the binary tree’s linked
list.
Add the correct pointer values to complete the diagram, using your answer from
part (a)(i).
RootPointer
Index
LeftPointer
Data
RightPointer
0
0
36
1
45
FreePointer
2
21
3
65
4
66
5
13
6
54
7
53
8
34
9
[6]
(b) The company wants to implement a program for the marking system. It will do this with
object-oriented programming (OOP).
Many candidates take the examination. Each examination paper is given a PaperID that is
made up of the centre (school) number followed by the candidate number.
Each examination paper is awarded a grade.
The following diagram shows the design for the ExaminationPaper class. This includes
the attributes and methods.
ExaminationPaper
FinalMark : INTEGER // maximum 2 digits, initialised to 0
Grade
: STRING // "Pass", "Merit", "Distinction"
// or "Fail", initialised to "Fail"
PaperID
: STRING // centre number followed by the
// candidate number, for example
// "ZZ00991001"
Create()
//
creates and initialises a new instance
//
of the ExaminationPaper class using
//
language-appropriate constructor
SetFinalMark()
//
checks that the mark parameter has a
//
valid value, if so, assigns it to
//
FinalMark
SetGrade()
//
sets Grade based on FinalMark
GetFinalMark()
//
returns FinalMark
GetGrade()
//
returns Grade
GetPaperID()
//
returns PaperID
(i)
The constructor receives the centre number and candidate number as parameter values
to create PaperID. Other properties are initialised as instructed in the class diagram.
Write program code for the Create() constructor method.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
… [5]
(ii)
Get and set methods are used to support the security and integrity of data in
object-oriented programming.
Explain how get and set methods are used to support security and integrity.
…
Question text
2
A stack is an Abstract Data Type (ADT).
(a) Tick (3) one box to show the statement that describes a stack data structure.
Statement
Tick (3)
Last in first out
First in first out
Last in last out
[1]
(b) A stack contains the values 20, 35, 43, 55.
55
Top of stack
43
35
20
(i)
Show the contents of the stack in part (b) after the following operations.
POP()
POP()
PUSH(10)
[1]
(ii)
Show the contents of the stack from part (b)(i) after these further operations:
POP()
PUSH(50)
PUSH(55)
POP()
PUSH(65)
[1]
(iii)
The stack is implemented as a 1D array, with eight elements, and given the identifier
ArrayStack.
The global variable Top contains the index of the last element in the stack, or −1 if the
stack is empty.
The function Push():
•
takes as a parameter an INTEGER value to place on the stack
•
adds the value to the top of the stack and returns TRUE to show that the operation
was successful
•
returns FALSE if the stack is full.
Write an algorithm in pseudocode for the function Push().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [7]
Question text
4
(a) A program has sorted some data in the array, List, in ascending order.
The following binary search algorithm is used to search for a value in the array.
FALSE
01
ValueFound
LengthOfList - 1
02
UpperBound
0
03
LowerBound
FALSE
04
NotInList
05
06
WHILE ValueFound = FALSE AND NotInList = FALSE
ROUND((LowerBound + UpperBound) / 2)
07
MidPoint
08
09
IF List[LowerBound] = SearchValue
10
THEN
TRUE
11
ValueFound
12
ELSE
13
IF List[MidPoint] < SearchValue
14
THEN
MidPoint + 1
15
UpperBound
16
ELSE
MidPoint – 1
17
UpperBound
18
ENDIF
19
IF LowerBound > MidPoint
20
THEN
TRUE
21
NotInList
22
ENDIF
23
ENDIF
24
ENDWHILE
25
26
IF ValueFound = FALSE
27
THEN
28
OUTPUT "The value is in the list"
29
ELSE
30
OUTPUT "The value is not found in the list"
31
ENDIF
Note:
The pseudocode function
ROUND(Reall : REAL) RETURNS INTEGER
rounds a number to the nearest integer value.
For example: ROUND(4.5) returns 5 and ROUND(4.4) returns 4
(i)
There are four errors in the algorithm.
Write the line of code where an error is present and write the correction in pseudocode.
Error 1 …
Correction …
Error 2 …
Correction …
Error 3 …
Correction …
Error 4 …
Correction …
[4]
(ii)
A binary search is one algorithm that can be used to search an array.
Identify another searching algorithm.
… [1]
(b) The following is an example of a sorting algorithm. It sorts the data in the array ArrayData.
""
01
TempValue
02
REPEAT
TRUE
03
Sorted
0 TO 4
04
FOR Count
05
IF ArrayData[Count] > ArrayData[Count + 1]
06
THEN
ArrayData[Count + 1]
07
TempValue
ArrayData[Count]
08
ArrayData[Count + 1]
TempValue
09
ArrayData[Count]
FALSE
10
Sorted
11
ENDIF
12
ENDFOR
13
UNTIL Sorted = TRUE
(i)
Complete the trace table for the algorithm given in part (b), for the ArrayData values
given in the table.
ArrayData
Count
TempValue
Sorted
0
1
2
3
4
5
5
20
12
25
32
29
[4]
(ii)
Rewrite lines 4 to 12 of the algorithm in part (b) using a WHILE loop instead of a FOR
loop.
…
…
…
…
…
…
…
Question text
6
An Abstract Data Type (ADT) is used to create a linked list. The linked list is created as an array of
records. The records are of type ListNode.
An example of a record of ListNode is shown in the following table.
Data Field
Value
Player
"Alvaro"
Pointer
1
(a) (i)
Use pseudocode to write a definition for the record type, ListNode.
…
…
…
…
… [3]
(ii)
An array, Scorers, will hold 10 nodes of type ListNode. Use pseudocode to write an
array declaration for this array. The lower bound subscript is 0.
… [2]
(b) The linked list stores ListNode records in alphabetical order of player. The last node in the
linked list always has a Pointer value of –1. The position of the first node in the linked list is
held in the variable ListHead.
After some processing, the array and variables are in the state as follows:
Scorers
ListHead
Player
Pointer
0
0
"Alvaro"
1
1
"Antoine"
3
2
"Dimitri"
7
3 "Cristiano"
2
4
"Gareth"
5
5
"Graziano"
6
6
"Olivier"
8
7
"Erik"
4
8
"Yaya"
9
9
"Zoto"
-1
A recursive function traverses the linked list to search for a player.
An example of calling the function, using pseudocode, is:
SearchList("Gareth", ListHead)
Position
Complete the following pseudocode to implement the function SearchList().
The function will return a value of 99 when a player is not found.
FUNCTION SearchList(Find : STRING, Position : INTEGER) RETURNS INTEGER
IF Scorer[Position].Player = …
THEN
RETURN …
ELSE
IF Scorer[Position].Pointer <> –1
THEN
SearchList(Find, … )
Position
RETURN …
ELSE
RETURN …
ENDIF
ENDIF
ENDFUNCTION
[5]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge International
Examinations Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download at www.cie.org.uk after
the live examination series.
Cambridge International Examinations is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of University of Cambridge Local
Question text
3
The arrays PollData[1:10] and CardData[1:10] store data.
PollData
12
85
52
57
25
11
33
59
56
91
CardData
11
12
25
33
52
56
57
59
91
85
An insertion sort sorts these data.
(a) State why it will take less time to complete an insertion sort on CardData than on PollData.
…
… [1]
(b) The following pseudocode algorithm performs an insertion sort on the CardData array.
Complete the following pseudocode algorithm.
01
ArraySize ← 10
FOR Pointer ← 2 TO …
02
03
ValueToInsert ← CardData[Pointer]
HolePosition ← …
04
WHILE (HolePosition > 1 AND ( … > … ))
05
CardData[HolePosition] ← CardData[ … ]
06
HolePosition ← …
07
08
ENDWHILE
CardData[HolePosition] ← …
09
10
ENDFOR
[7]
(c) (i)
A binary search algorithm is used to find a specific value in an array.
Explain why an array needs to be sorted before a binary search algorithm can be used.
…
…
…
…
…
…
…
… [2]
(ii)
The current contents of CardData are shown.
11
12
25
33
52
56
57
59
85
91
Explain how a binary search will find the value 25 in CardData.
…
…
…
…
…
…
…
…
Question text
6
An Abstract Data Type (ADT) is used to create an unordered binary tree. The binary tree is created
as an array of nodes. Each node consists of a data value and two pointers.
A record type, Node, is declared using pseudocode.
TYPE Node
DECLARE DataValue : STRING
DECLARE LeftPointer : INTEGER
DECLARE RightPointer : INTEGER
ENDTYPE
The following statement declares an array BinaryTree.
DECLARE BinaryTree : ARRAY[0:14] OF Node
A variable, NextNode, points to the next free node.
The following diagram shows a possible node.
ArrayIndex
DataValue
LeftPointer
RightPointer
0
"Red"
1
2
The commands in the following table create and add nodes to the binary tree.
Command
Comment
CreateTree(NodeData)
Sets NextNode to 0.
Writes NodeData into DataValue at the position NextNode
Updates NextNode using NextNode = NextNode + 1
AttachLeft(NodeData, ParentNode)
Writes NodeData into DataValue of NextNode
Sets the LeftPointer of node ParentNode to NextNode
Updates NextNode using NextNode = NextNode + 1
AttachRight(NodeData, ParentNode)
Writes NodeData into DataValue of NextNode
Sets the RightPointer of node ParentNode to NextNode
Updates NextNode using NextNode = NextNode + 1
(a) The following commands are executed.
CreateTree("Red")
AttachLeft("Blue", 0)
AttachRight("Green", 0)
The following diagram shows the current state of the binary tree.
0
"Red"
1
2
1
"Blue"
2 "Green"
…
…
…
…
…
…
…
…
…
…
…
…
Write on the diagram to show the state of the binary tree after the following commands have
been executed.
AttachRight("Black", 2)
AttachLeft("Brown", 2)
AttachLeft("Peach", 3)
AttachLeft("Yellow", 1)
AttachRight("Purple", 1)
AttachLeft("White", 6)
AttachLeft("Pink", 7)
AttachLeft("Grey", 9)
AttachRight("Orange", 9)
[5]
(b) A new command has been added to initialise the pointers of the binary tree to − 1 to indicate
they are not in use.
A leaf is a node of the binary tree which has no children. In the case of this binary tree, a node
with a LeftPointer of − 1 and a RightPointer of − 1 is a leaf.
Write a recursive function, in program code, to traverse the binary tree and output the value
of DataValue for each leaf node.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
…
…
Question text
1
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
Immediate addressing. Load the number n to ACC.
LDM
#n
Direct addressing. Load the contents of the location at the given address
LDD
<address>
to ACC.
Indirect addressing. The address to be used is at the given address.
LDI
<address>
Load the contents of this second address to ACC.
Indexed addressing. Form the address from <address> + the contents of
LDX
<address>
the index register. Copy the contents of this calculated address to ACC.
Store the contents of ACC at the given address.
STO
<address>
Add 1 to the contents of the register (ACC or IX).
INC
<register>
Subtract 1 from the contents of the register (ACC or IX).
DEC
<register>
Compare the contents of ACC with the contents of <address>.
CMP
<address>
Jump to given address.
JMP
<address>
Following a compare instruction, jump to <address> if the compare was
JPE
<address>
True.
Following a compare instruction, jump to <address> if the compare was
JPN
<address>
False.
Bitwise AND operation of the contents of ACC with the contents of
AND
<address>
<address>.
Bitwise XOR operation of the contents of ACC with the contents of
XOR
<address>
<address>.
Bitwise OR operation of the contents of ACC with the contents of
OR
<address>
<address>.
Key in a character and store its ASCII value in ACC.
IN
Output to the screen the character whose ASCII value is stored in ACC.
OUT
Return control to the operating system.
END
(a) A programmer writes a program that:
•
reads a character from the keyboard (assume it will be a capital letter)
•
outputs the alphabetical sequence of characters from ‘A’ to the character input. For
example, if the character ‘G’ is input, the output is:
ABCDEFG
The programmer has started to write the program in the table on the following page. The
Comment column contains descriptions for the missing instructions, labels and data.
Complete the following program. Use op codes from the given instruction set.
Label
Op code
Operand
Comment
START:
// INPUT character
// store in CHAR
// Initialise ACC (ASCII value for 'A' is 65)
// OUTPUT ACC
// compare ACC with CHAR
// if equal jump to end of FOR loop
// increment ACC
// jump to LOOP
ENDFOR:
END
CHAR:
[8]
(b) The programmer now starts to write a program that:
•
tests whether an 8-bit two’s complement integer stored at address NUMBER is positive or
negative
•
outputs 'P' for a positive integer and 'N' for a negative integer.
Complete the following program. Use op codes from the given instruction set.
Show the required value of MASK in binary.
Label
Op code
Operand
Comment
START:
MASK
// set to zero all bits except sign bit
// compare with 0
// if not equal jump to ELSE
THEN:
// load ACC with 'P' (ASCII value 80)
JMP
ENDIF
ELSE:
// load ACC with 'N' (ASCII value 78)
ENDIF:
END
NUMBER:
B00000101
// integer to be tested
MASK:
// value of mask in binary
[7]
Question text
2
A hash table has these associated operations:
•
create hash table
•
insert record
•
search hash table
A hash table is to be used to store customer records.
Each record consists of a unique customer ID, the record key, and other customer data.
(a) The following pseudocode declares a customer record structure.
TYPE CustomerRecord
CustomerID : INTEGER
Data : STRING
ENDTYPE
The hash table is to be implemented as a 1D array Customer with elements indexed
0 to 199. The procedure to create a hash table will declare and initialise the array by storing
200 records with the CustomerID field in each record set to 0.
Complete the pseudocode.
PROCEDURE CreateHashTable()
…
…
…
…
[4]
ENDPROCEDURE
(b) A hashing function Hash exists, which takes as a parameter the customer ID and returns an
integer in the range 0 to 199 inclusive.
(i)
The procedure, InsertRecord, takes as a parameter the customer record to be
inserted into the hash table.
The procedure makes use of the function Hash. Collisions will be managed using open
hashing. This means a collision is resolved by storing the record in the next available
location. The procedure will generate an error message if the hash table is full.
Complete the pseudocode for the procedure.
PROCEDURE InsertRecord(BYVALUE NewCustomer : CustomerRecord)
FALSE
TableFull
// generate hash value
…
Index
Index
// initialise Pointer variable to hash value
Pointer
// find a free table element
WHILE …
…
Pointer
// wrap back to beginning of table if necessary
IF …
THEN
…
ENDIF
// check if back to original index
IF …
THEN
TRUE
TableFull
ENDIF
ENDWHILE
IF …
THEN
…
ELSE
…
ENDIF
[9]
ENDPROCEDURE
(ii)
The function SearchHashTable will search for a record in the hash table. The function
takes as a parameter the customer ID to be searched for. The function will return the
position in the hash table where the record has been saved. If the hash table does not
contain the record, the function will return the value −1.
You can assume that there is at least one empty record in the hash table.
Complete the pseudocode for the function.
FUNCTION SearchHashTable(BYVALUE SearchID : INTEGER) RETURNS INTEGER
// generate hash value
…
Index
// check each record from index until found or not there
WHILE ( …