Chapter 19 SB Answers
Chapter 19 SB Answers
Cambridge International AS & A Level Computer Science Answers
4
DECLARE myList : ARRAY[0:19] OF INTEGER
DECLARE upperBound : INTEGER
DECLARE lowerBound : INTEGER
DECLARE index : INTEGER
DECLARE swap : BOOLEAN
DECLARE temp : INTEGER
DECLARE top : INTEGER
upperBound 19
lowerBound 0
top upperBound
REPEAT
Swap FALSE
FOR index = lowerBound TO top - 1
IF myList[index] > myList[index + 1]
THEN
temp myList[index]
myList[index] myList[index + 1]
myList[index + 1] temp
swap TRUE
ENDIF
NEXT
top top -1
UNTIL (NOT swap) OR (top = 0)
5 Answers given
Activity 19A
Python
#Python program for Linear Search
#create array to store all the numbers
myList = [4, 2, 8, 17, 9, 3, 7, 12, 34, 21]
found = False
if(found):
print("Item found")
else:
print("Item not found")
Cambridge International AS & A Level Computer Science Answers
VB
'VB program for Linear Search
Module Module1
Public Sub Main()
Dim index As Integer
Dim item As Integer
Dim found As Boolean
'Create array to store all the numbers
Dim myList() As Integer = New Integer() {4, 2, 8, 17, 9, 3, 7, 12, 34,
21}
If (found) Then
Console.WriteLine("Item found")
Else : Console.WriteLine("Item not found")
End If
Console.ReadKey()
End Sub
End Module
Java
//Java program Linear Search
import java.util.Scanner;
class ACTIVITY19A
{
public static void main(String args[])
{
Scanner myObj = new Scanner(System.in);
//Create array to store the all the numbers
int myList[] = new int[] {4, 2, 8, 17, 9, 3, 7, 12, 34, 21};
int item, index;
boolean found = false;
}
}
Cambridge International AS & A Level Computer Science Answers
Activity 19B
4 comparisons to find the letter D
A and B take fewer comparisons
Activity 19C
Python
#Python program for Binary Search
#create sorted list to store all the numbers
myList = [16, 19, 21, 27, 36, 42, 55, 67, 76, 89]
found = False
lowerBound = 0
upperBound = len(myList) - 1
if(found):
print("Item found")
else:
print("Item not found")
Cambridge International AS & A Level Computer Science Answers
VB
'VB program for Binary Search
Module Module1
Public Sub Main()
Dim index, lowerBound, upperBound As Integer
Dim item As Integer
Dim found As Boolean
'Create array to store all the numbers
Dim myList() As Integer = New Integer() {16, 19, 21, 27, 36, 42,
55, 67, 76, 89}
found = False
lowerBound = 0
upperBound = myList.Length - 1
Do
index = (upperBound + lowerBound) \ 2
If (found) Then
Console.WriteLine("Item found")
End If
Console.ReadKey()
End Sub
End Module
Cambridge International AS & A Level Computer Science Answers
Java
//Java program Binary Search
import java.util.Scanner;
class ACTIVITY19C
{
public static void main(String args[])
{
Scanner myObj = new Scanner(System.in);
//Create array to store the all the numbers
int myList[] = new int[] {16, 19, 21, 27, 36, 42, 55, 67, 76, 89};
do
{
index = (upperBound + lowerBound) / 2;
if (myList[index] == item)
{
found = true;
}
if (item > myList[index])
{
lowerBound = index + 1;
}
if (item < myList[index])
{
upperBound = index - 1;
}
if (found)
{
System.out.println("Item found");
}
else
{
System.out.println("Item not found");
}
}
}
Cambridge International AS & A Level Computer Science Answers
Activity 19D
Python
#Python program for Insertion Sort
myList = [4,46,43,27,57,41,45,21,14]
lowerBound = 0
upperBound = len(myList)
VB
'VB program for insertion sort
Module Module1
Sub Main()
Dim myList() As Integer = New Integer() {4, 46, 43, 27, 57, 41, 45, 21, 14}
Dim index, lowerBound, upperBound, place, myKey, temp As Integer
upperBound = myList.Length - 1
lowerBound = 0
End Sub
End Module
Cambridge International AS & A Level Computer Science Answers
Java
public class ACTIVITY19D {
int insertPlace = 0;
for (int place = index-1; place >= lowerBound; place--) {
if (myList[place] > myKey) {
myList[place+1] = myList[place];
} else {
insertPlace = place+1;
break;
}
}
myList[insertPlace] = myKey;
}
Cambridge International AS & A Level Computer Science Answers
Activity 19E
Python
stack = [None for index in range(0,10)]
basePointer = 0
topPointer = -1
stackFull = 10
item = None
def push(item):
global topPointer
if topPointer < stackFull - 1:
topPointer = topPointer + 1
stack[topPointer] = item
else:
print("Stack is full, cannot push")
def pop():
global topPointer, basePointer, item
if topPointer == basePointer -1:
print("Stack is empty,cannot pop")
else:
item = stack[topPointer]
topPointer = topPointer - 1
push(7)
push(32)
print (*stack)
pop()
print(item)
pop()
print(item)
pop()
print(*stack)
push(1)
print(*stack)
push(2)
print(*stack)
push(3)
print(*stack)
push(4)
print(*stack)
push(5)
print(*stack)
push(6)
print(*stack)
push(7)
print(*stack)
push(8)
print(*stack)
push(9)
print(*stack)
push(10)
print(*stack)
push(11)
Cambridge International AS & A Level Computer Science Answers
Note:
There is an automated method for printing the stack in Python that has been included in the program.
For VB and Java you could write the function that would print the stack, using a loop and use it to
show the contents of the stack at each stage.
VB
'VB program for stack
Module Module1
Public stack() As Integer = {Nothing, Nothing, Nothing, Nothing,
Nothing, Nothing, Nothing, Nothing, Nothing, Nothing,
Nothing}
Public basePointer As Integer = 0
Public topPointer As Integer = -1
Public Const stackFull As Integer = 10
Public item As Integer
Sub Main()
push(7)
push(32)
pop()
Console.WriteLine(item)
pop()
Console.WriteLine(item)
pop()
push(1)
push(2)
push(3)
push(4)
push(5)
push(6)
push(7)
push(8)
push(9)
push(10)
push(11)
Console.ReadKey() 'wait for keypress
End Sub
Sub pop()
If topPointer = basePointer - 1 Then
Console.WriteLine("Stack is empty, cannot pop")
Else
item = Stack(topPointer)
topPointer = topPointer - 1
End If
End Sub
Cambridge International AS & A Level Computer Science Answers
Java
//Java program for a stack
import java.util.Scanner;
class ACTIVITY19E
{
public static int stack[] = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
public static int basePointer = 0;
public static int topPointer = -1;
public static final int stackFull = 10;
public static int item;
pop();
System.out.println(item);
pop();
push(1);
push(2);
push(3);
push(4);
push(5);
push(6);
push(7);
push(8);
push(9);
push(10);
push(11);
}
}
Cambridge International AS & A Level Computer Science Answers
Activity 19F
Python
queue = [None for index in range(0,10)]
frontPointer = 0
rearPointer = -1
queueFull = 10
queueLength = 0
def enQueue(item):
global queueLength, rearPointer
if queueLength < queueFull:
if rearPointer < len(queue) - 1:
rearPointer = rearPointer + 1
else:
rearPointer = 0
queueLength = queueLength + 1
queue[rearPointer] = item
else:
print("Queue is full, cannot enqueue")
def deQueue():
global queueLength, frontPointer, item
if queueLength == 0:
print("Queue is empty,cannot dequeue")
else:
item = queue[frontPointer]
queue[frontPointer] = None
if frontPointer == len(queue) - 1:
frontPointer = 0
else:
frontPointer = frontPointer + 1
queueLength = queueLength -1
enQueue(7)
enQueue(32)
print (*queue)
deQueue()
print(item)
deQueue()
print(item)
deQueue()
print(*queue)
enQueue(1)
print(*queue)
enQueue(2)
print(*queue)
enQueue(3)
print(*queue)
enQueue(4)
print(*queue)
enQueue(5)
print(*queue)
enQueue(6)
print(*queue)
enQueue(7)
print(*queue)
enQueue(8)
print(*queue)
enQueue(9)
print(*queue)
enQueue(10)
print(*queue)
enQueue(11)
Cambridge International AS & A Level Computer Science Answers
Note:
There is an automated method for printing the stack in Python that has been included in the program.
For VB and Java you could write the function that would print the stack, using a loop and use it to
show the contents of the stack at each stage.
VB
'VB program for a queue
Module Module1
Console.ReadKey()
enQueue(7)
enQueue(32)
deQueue()
Console.WriteLine(item)
deQueue()
Console.WriteLine(item)
deQueue()
Console.ReadKey()
enQueue(1)
enQueue(2)
enQueue(3)
enQueue(4)
enQueue(5)
enQueue(6)
enQueue(7)
enQueue(8)
enQueue(9)
enQueue(10)
enQueue(11)
Console.ReadKey()
End Sub
Sub enQueue(ByVal item)
If queueLength < queueFull Then
If rearPointer < queue.length - 1 Then
rearPointer = rearPointer + 1
Else
rearPointer = 0
End If
queueLength = queueLength + 1
queue(rearPointer) = item
Cambridge International AS & A Level Computer Science 13
© Helen Williams and David Watson 2020
Cambridge International AS & A Level Computer Science Answers
Else
Console.WriteLine("Queue is full, cannot enqueue")
End if
End Sub
Sub deQueue()
If queueLength = 0 Then
Console.WriteLine("Queue is empty, cannot dequeue")
Else
item = queue(frontPointer)
If frontPointer = queue.length - 1 Then
frontPointer = 0
Else
frontPointer = frontPointer + 1
End if
queueLength = queueLength - 1
End If
End Sub
End Module
Java
//Java program for a queue
import java.util.Scanner;
class Queue
{
public static int queue[] = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
public static int frontPointer = 0;
public static int rearPointer = -1;
public static final int queueFull = 10;
public static int queueLength = 0;
public static int item;
Cambridge International AS & A Level Computer Science Answers
deQueue();
System.out.println(item);
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
enQueue(6);
enQueue(7);
enQueue(8);
enQueue(9);
enQueue(10);
enQueue(11);
}
}
Cambridge International AS & A Level Computer Science Answers
myLinkedList = [27, 19, 36, 42, 16, None, None, None, None, None, None,
None]
myLinkedListPointers = [-1, 0, 1, 2, 3, 6, 7, 8, 9, 10, 11, -1]
heapStartPointer = 5
startPointer = 4
nullPointer = -1
def delete(itemDelete):
global startPointer, heapStartPointer
if startPointer == nullPointer:
print("Linked List empty")
else:
index = startPointer
while myLinkedList[index] != itemDelete and index !=
nullPointer:
oldindex = index
index = myLinkedListPointers[index]
if index == nullPointer:
print("Item ", itemDelete, " not found")
else:
myLinkedList[index] = None
tempPointer = myLinkedListPointers[index]
myLinkedListPointers[index] = heapStartPointer
heapStartPointer = index
myLinkedListPointers[oldindex] = tempPointer
def insert(itemAdd):
global startPointer, heapStartPointer
if heapStartPointer == nullPointer:
print("Linked List full")
else:
tempPointer = startPointer
startPointer = heapStartPointer
heapStartPointer = myLinkedListPointers[heapStartPointer]
myLinkedList[startPointer] = itemAdd
myLinkedListPointers[startPointer] = tempPointer
def find(itemSearch):
found = False
itemPointer = startPointer
while itemPointer != nullPointer and not found:
if myLinkedList[itemPointer] == itemSearch:
found = True
else:
itemPointer = myLinkedListPointers[itemPointer]
return itemPointer
Cambridge International AS & A Level Computer Science Answers
VB
'VB program for a linked list
Module Module1
Public Dim myLinkedList() As Integer = {27, 19, 36, 42, 16, Nothing,
Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}
Public Dim myLinkedListPointers() As Integer = {-1, 0, 1, 2, 3, 6, 7, 8,
9, 10, 11, -1}
Cambridge International AS & A Level Computer Science Answers
End if
End Sub
Cambridge International AS & A Level Computer Science Answers
myLinkedList(index) = nothing
tempPointer = myLinkedListPointers(index)
myLinkedListPointers(index) = heapStartPointer
heapStartPointer = index
myLinkedListPointers(oldIndex) = tempPointer
End if
End If
End Sub
End Module
Java
//Java program for a linked list
import java.util.Scanner;
class ACTIVITY19GHI
{
public static int myLinkedList[] = new int[] {27, 19, 36, 42, 16, 0, 0,0, 0,
0, 0};
public static int myLinkedListPointers[] = new int[] {-1, 0, 1, 2, 3, 6, 7, 8,
9, 10, 11, -1};
public static int startPointer = 4;
public static int heapStartPointer = 5;
public static final int nullPointer = -1;
if (startPointer == nullPointer)
System.out.println("Linked List is empty");
else
{
Cambridge International AS & A Level Computer Science Answers
{
myLinkedList[index] = 0;
int tempPointer = myLinkedListPointers[index];
myLinkedListPointers[index] = heapStartPointer;
heapStartPointer = index;
myLinkedListPointers[oldIndex] = tempPointer;
}
}
}
Cambridge International AS & A Level Computer Science Answers
System.out.println();
if (result != -1 )
{
System.out.println("Item found");
}
else
{
System.out.println("Item not found");
}
}
}
For 19H
startPointer heapStartPointer itemAdd tempPointer
Already set to 4 Already set to 5 18
5 6 4
6 7 25 5
The linked list, myLinkedList will now be as shown below.
mylinkedList myLinkedListPointers
[0] 27 −1
[1] 19 0
[2] 36 1
[3] 42 2
[4] 16 3
[5] 18 4
startPointer → [6] 25 5
heapStartPointer → [7] 8
[8] 9
[9] 10
[10] 11
[11] −1
Cambridge International AS & A Level Computer Science 21
© Helen Williams and David Watson 2020
Cambridge International AS & A Level Computer Science Answers
For 19I
startPointer heapStartPointer itemDelete index oldIndex tempPointer
Already set to 6 Already set to 7 5 5
6 4 18 4 4 2
3
mylinkedList myLinkedListPointers
[0] 27 −1
[1] 19 0
[2] 36 1
[3] 42 2
heapStartPointer → [4] 16 7
[5] 18 3
startPointer → [6] 25 5
[7] 8
[8] 9
[9] 10
[10] 11
[11] −1
Activity 19J
/
‐ +
x y * z
x y
Activity 19K
TYPE node
DECLARE item : STRING
DECLARE leftPointer : INTEGER
DECLARE rightPointer : INTEGER
ENDTYPE
DECLARE myTree[0 : 49] OF node
DECLARE rootPointer : INTEGER
DECLARE nextFreePointer : INTEGER
Cambridge International AS & A Level Computer Science Answers
Activity 19L
rootPointer itemPointer itemSearch
0 0 55
2
3
5
8
Activity 19M
leftBranch nextFreePointer itemAddPointer rootPointer itemAdd itemPointer oldPointer
10 10 0 25 0 0
11
TRUE 1 1
FALSE 6 6
FALSE −1
[0] 27 1 2
[1] 19 4 6
[2] 36 −1 3
[3] 42 −1 5
[4] 16 −1 7
[5] 89 8 −1
[6] 21 −1 10
[7] 17 −1 −1
[8] 55 −1 −1
[9] 18 −1 −1
[10] 25 −1 −1
[11] −1
Cambridge International AS & A Level Computer Science Answers
Activity 19N
Path = {School, Town centre, Shopping centre, Gardens}
Path = {Town centre, School, Train station}
Path = {Town centre, Shopping centre, Gardens, Town centre}
Activity 19O
// a linked list to store names
TYPE linkedList
DECLARE name : STRING
DECLARE pointer : INTEGER
ENDTYPE
REPEAT
index startPointer
OUTPUT myLinkedList[index].name
Index myLinkedList[index].pointer
UNTIL index = -1
Cambridge International AS & A Level Computer Science Answers
Activity 19P
Python
#using a dictionary in Python
studentdict ={
"Leon":27,
"Ahmad":78,
"Susie":64
}
print (studentdict)
score = studentdict["Leon"]
print(score)
studentdict["Mo"] = 99
print (studentdict)
del studentdict["Ahmad"]
print (studentdict)
VB
'using a dictionary in VB
Module Module1
Sub Main()
Dim studentdict As New Dictionary(Of String, Integer)
Dim studentScore As Integer
Dim studentName As String
studentdict.Add("Leon", 27)
studentdict.Add("Ahmad", 78)
studentdict.Add("Susie", 64)
For Each item As KeyValuePair(Of String, Integer) In studentdict
studentName = item.Key
studentScore = item.Value
Console.WriteLine(studentName + " " + studentScore.ToString)
Next
If studentdict.ContainsKey("Ahmad") Then
studentScore = studentdict.Item("Ahmad")
Console.WriteLine(studentScore)
End If
Console.WriteLine()
studentdict.Remove("Ahmad")
For Each item As KeyValuePair(Of String, Integer) In studentdict
studentName = item.Key
studentScore = item.Value
Console.WriteLine(studentName + " " + studentScore.ToString)
Next
Console.ReadKey()
End Sub
End Module
Cambridge International AS & A Level Computer Science Answers
Java
//Java program for a dictionary
import java.util.*;
class ACTIVITY19P
{
public static void main(String[] args)
{
// creating a My HashTable Dictionary
Hashtable<String, Integer> studentdict = new
Hashtable<String, Integer>();
System.out.println(studentdict);
Activity 19Q
1 a) see Figure 19.10
b) see Figure 10.12
2 a) an ADT that consists of pairs a key and a value, they key is used to find the value
b)
TYPE linkedList
DECLARE item : STRING
DECLARE Pointer : INTEGER
ENDTYPE
TYPE dictionary
DECLARE key : myLinkedList : ARRAY [0:19] OF linkedList
DECLARE value : ARRAY [0:19] OF STRING
ENDTYPE
Cambridge International AS & A Level Computer Science Answers
3 For a linear search the time to perform a search will increase linearly as the number of items in the
list increases, the time taken is directly proportional to the number of items in the list. In big O
notation, this is O(N) where N is the number of items in the list.
For a binary search the time to perform a search will increase linearly as the number of items in
the list increases exponentially, the time taken is logarithmically proportional to the number of
items in the list. In big O notation, this is O(log N) where N is the number of items in the list.
Therefore a binary search becomes more time efficient than a linear search as the number of items
in the list increases.
Activity 19R
Python
#Python program recursive factorial function
def factorial(number):
if number == 0:
answer = 1
else:
answer = number * factorial(number - 1)
return answer
print(factorial(0))
print(factorial(5))
VB
'VB program recursive factorial function
Module Module1
Sub Main()
Console.WriteLine(factorial(0))
Console.Writeline(factorial(5))
Console.ReadKey()
End Sub
Function factorial(ByVal number As Integer) As Integer
Dim answer As Integer
If number = 0 Then
answer = 1
Else
answer = number * factorial(number - 1)
End If
return answer
End Function
End Module
Cambridge International AS & A Level Computer Science Answers
Java
// Java program recursive factorial function
public class Factorial {
1 factorial(5) 5 5 * factorial(4)
2 factorial(4) 4 4 * factorial(3)
3 factorial(3) 3 3 * factorial(2)
4 factorial(2) 2 2 * factorial(1)
5 factorial(1) 1 1 * factorial(0)
6 factorial(0) 0 1 1
5 continued factorial(1) 1 1 * 1 1
4 continued factorial(2) 2 2 * 1 2
3 continued factorial(3) 3 3 * 2 6
2 continued factorial(4) 4 4 * 3 * 2 24
Activity 19T
1 Recursion – see 19.2.1
2 Use of stack for recursive procedures – see 19.2.2
Cambridge International AS & A Level Computer Science Answers
Activity 19S
FUNCTION fibonacci (number : INTEGER) RETURNS INTEGER
IF number = 0 OR number = 1
THEN
answer 1 // base case
ELSE
answer Fibonacci(number - 1) + fibonacci (number - 2)
// recursive call with general case
ENDIF
RETURN answer
ENDFUNCTION
1 fibonacci(4) 4 fibonacci(3) +
fibonacci(2)
2 fibonacci(3) 3 fibonacci(2) +
fibonacci(1)
3 fibonacci(2) 2 fibonacci(1) +
fibonacci(0)
4 fibonacci(1) 1 1 1
5 fibonacci(0) 0 1 1
3 continued fibonacci(2) 2 1 + 1 2
2 continued fibonacci(3) 3 1 + 2 3
1 continued fibonacci(4) 4 3 + 2 5
Cambridge International AS & A Level Computer Science Answers
b) ii) The outer loop is always executed 9 times and the inner loop is never executed as
Temp is always larger.
c) i) Both loops are always executed 9 times.
ii)
REPEAT
NoMoreSwaps TRUE
FOR Pointer 1 TO NumberOfItems – 1
IF NameList[Pointer] > NameList[Pointer + 1]
THEN
NoMoreSwaps FALSE
Temp NameList[Pointer]
NameList[Pointer] NameList[Pointer + 1]
NameList[Pointer + 1] Temp
ENDIF
ENDFOR
NumberOfItems NumberOfItems - 1
UNTIL NoMoreSwaps
Cambridge International AS & A Level Computer Science Answers
2 a)
Head Ben
Ahmed
Tail Jatinder 0
b) i)
Name Pointer
HeadPointer [1] 2
0 [2] 3
[3] 4
TailPointer [4] 5
0 [5] 6
[6] 7
FreePointer [7] 8
1 [8] 9
[9] 10
[10] 0
Cambridge International AS & A Level Computer Science Answers
ii)
PROCEDURE RemoveName()
//Report error if Queue is empty
IF HeadPointer = 0
THEN
Error
ELSE
OUTPUT Queue[HeadPointer].Name
//current node is head of queue
CurrentPointer HeadPointer
// update head pointer
HeadPointer Queue[CurrentPointer].Pointer
//if only one element in queue, then update tail pointer
IF HeadPointer = 0
THEN
TailPointer 0
ENDIF
// link released node to free list
Queue[CurrentPointer].Pointer FreePointer
FreePointer CurrentPointer
ENDIF
ENDPROCEDURE