9618/43 · October/November 2022 · Worked solutions

Step-by-step solutions based on the mark scheme (not a mark-scheme dump).

Question 1

Paper page 2 for Q1
Original paper page 2

Part 1(a) — solution

  1. (global) 1D (Integer) array DataArray
  2. 100 elements

Visual Basic (VB.NET)

Dim DataArray(99) As Integer

Python

DataArray = [0 for I in range (100)]

Mark points

  • (global) 1D (Integer) array DataArray
  • 100 elements

Part 1(b) — solution

  1. Procedure ReadFile() header (and end where appropriate)
  2. opening file IntegerData.txt (for read)
  3. looping through the 100 elements // looping to end of file
  4. reading each (and all) value from file and storing in array
  5. closing file (in appropriate place)

Mark points

  • Procedure ReadFile() header (and end where appropriate)
  • opening file IntegerData.txt (for read)
  • looping through the 100 elements // looping to end of file
  • reading each (and all) value from file and storing in array
  • closing file (in appropriate place)

Part 1 — solution

  1. Exception Handling (for opening the file, or for reading values from the
  2. …with appropriate catch and output messages

Python

def ReadFile():
                 global DataArray

                 try:
                   TextFile = "IntegerData.txt"
                   File = open(TextFile, 'r')
                   for X in range(0, 100):
                       DataArray[X] = File.readline()
                       DataArray[X].rstrip('\n')
                       DataArray[X] = int(DataArray[X])
                   File.close()
                 except IOError:
                   print("Count not find file")

 2022                                   Page 3 of 23

9618/43                  Cambridge International AS & A Level – Mark Scheme    October/November
                                             PUBLISHED                                     2022

Mark points

  • Exception Handling (for opening the file, or for reading values from the
  • …with appropriate catch and output messages

Part 1(b) — solution

  1. Dim TextFile As String = "IntegerData.txt"
  2. For X = 0 To 99
  3. DataArray(X) = FileReader.ReadLine()

Visual Basic (VB.NET)

Sub ReadFile()
                 try
                    Dim TextFile As String = "IntegerData.txt"
                    Dim FileReader As New
               System.IO.StreamReader(TextFile)
                    For X = 0 To 99
                      DataArray(X) = FileReader.ReadLine()
                    Next
                      FileReader.Close()
                  Catch ex As Exception
                    Console.WriteLine("Invalid file")
                 End Try
               End Sub

Mark points

  • Dim TextFile As String = "IntegerData.txt"
  • For X = 0 To 99
  • DataArray(X) = FileReader.ReadLine()

Part 1(c) — solution

  1. Function FindValues() (and end where appropriate) and input of data
  2. …validation/casting(/storing as) of input as integer
  3. …validation of input between 1 and 100 (inclusive)
  4. looping through all 100 array elements…
  5. …comparing input to each array element…
  6. …initialising counter to 0 and then adding 1 each time it is found…
  7. Returning the total

Mark points

  • Function FindValues() (and end where appropriate) and input of data
  • …validation/casting(/storing as) of input as integer
  • …validation of input between 1 and 100 (inclusive)
  • looping through all 100 array elements…
  • …comparing input to each array element…
  • …initialising counter to 0 and then adding 1 each time it is found…
  • Returning the total

Part 1(c) — solution

  1. DataToFind = -1
  2. DataToFind = int(input("Enter a number between 1
  3. Total = 0
  4. if DataArray[X] == DataToFind:
  5. Total = Total + 1
  6. DataToFind = Console.ReadLine()
  7. Loop Until (DataToFind >= 1 And DataToFind <= 100)
  8. Dim Total As Integer = 0
  9. For X = 0 To 99
  10. If DataArray(X) = DataToFind Then
  11. Total = Total + 1

Visual Basic (VB.NET)

Function FindValues()
                 Dim DataToFind As Integer
                 Do
                    Console.WriteLine("Enter a number between 1 and 100")
                    DataToFind = Console.ReadLine()
                 Loop Until (DataToFind >= 1 And DataToFind <= 100)
                 Dim Total As Integer = 0
                    For X = 0 To 99
                      If DataArray(X) = DataToFind Then
                         Total = Total + 1
                      End If
                    Next
                 Return Total
               End Function

Python

def FindValues():
                 global DataArray

                   DataToFind = -1

                   while(DataToFind < 1 or DataToFind > 100):
                       DataToFind = int(input("Enter a number between 1
               and 100"))
                   Total = 0
                   for X in range(0, 99):
                       if DataArray[X] == DataToFind:
                           Total = Total + 1
                   return Total

Mark points

  • DataToFind = -1
  • DataToFind = int(input("Enter a number between 1
  • Total = 0
  • if DataArray[X] == DataToFind:
  • Total = Total + 1
  • DataToFind = Console.ReadLine()
  • Loop Until (DataToFind >= 1 And DataToFind <= 100)
  • Dim Total As Integer = 0
  • For X = 0 To 99
  • If DataArray(X) = DataToFind Then
  • Total = Total + 1

Part 1(d)(i) — solution

  1. Calling ReadFile() and then FindValues() (in the main program)
  2. storing/using return value from FindValues() …
  3. …outputting return value with appropriate message

Visual Basic (VB.NET)

Sub Main()
                 ReadFile()
                 Dim ReturnValue As Integer = FindValues()
                 Console.WriteLine("The number was found " & ReturnValue
               & " times")
               End Sub

Python

ReadFile()
               print("The number appears " + str(FindValues()) + "
               times")

Mark points

  • Calling ReadFile() and then FindValues() (in the main program)
  • storing/using return value from FindValues() …
  • …outputting return value with appropriate message

Part 1(e) — solution

  1. procedure declaration (and end where appropriate) and
  2. correct outer loop …
  3. … correct inner loop …
  4. … swapping all elements if in incorrect order

Python

def BubbleSort():
                 global DataArray

                   N = 100
                   for I in range(N-1):
                       for J in range(0, N-I-1):
                            if DataArray[J] > DataArray[J+1]:
                                DataArray[J], DataArray[J+1] =
               DataArray[J+1], DataArray[J]
               #main
               ReadFile()
               print("The number appears " + str(FindValues()) + "
               times")
               BubbleSort()
               print(DataArray)

Mark points

  • procedure declaration (and end where appropriate) and
  • correct outer loop …
  • … correct inner loop …
  • … swapping all elements if in incorrect order

Part 1(e) — solution

  1. Dim Outer As Integer = 100 - 1
  2. Inner = 0
  3. Swap = False
  4. Temp = DataArray(Inner)
  5. DataArray(Inner) = DataArray(Inner + 1)
  6. DataArray(Inner + 1) = Temp
  7. Swap = True
  8. Inner = Inner + 1
  9. Loop Until Inner = Outer
  10. Outer = Outer - 1
  11. Loop Until Swap = False Or Outer = 0
  12. For X = 0 To 99
  13. Dim ReturnValue As Integer = FindValues()

Visual Basic (VB.NET)

Sub Bubblesort()
                 Dim Outer As Integer = 100 - 1
                 Dim Swap As Boolean
                 Dim Inner As Integer
                 Dim Temp As Integer
                 Do
                  Inner = 0
                  Swap = False
                  Do
                     If DataArray(Inner) > DataArray(Inner + 1) Then
                       Temp = DataArray(Inner)
                       DataArray(Inner) = DataArray(Inner + 1)
                       DataArray(Inner + 1) = Temp
                       Swap = True
                     End If
                     Inner = Inner + 1
                     Loop Until Inner = Outer
                     Outer = Outer - 1
                 Loop Until Swap = False Or Outer = 0
                 For X = 0 To 99
                    Console.WriteLine(DataArray(X))
                 Next
               End Sub
               Sub Main()
                    ReadFile()
                    Dim ReturnValue As Integer = FindValues()
                    Console.WriteLine("The number was found " &
               ReturnValue & " times")
                    Bubblesort()
               End Sub

Mark points

  • Dim Outer As Integer = 100 - 1
  • Inner = 0
  • Swap = False
  • Temp = DataArray(Inner)
  • DataArray(Inner) = DataArray(Inner + 1)
  • DataArray(Inner + 1) = Temp
  • Swap = True
  • Inner = Inner + 1
  • Loop Until Inner = Outer
  • Outer = Outer - 1
  • Loop Until Swap = False Or Outer = 0
  • For X = 0 To 99
  • Dim ReturnValue As Integer = FindValues()

Part 1 — solution

  1. IF Root = -1 THEN
  2. IF ArrayNodes[Root,1] = ValueToFind THEN
  3. IF ArrayNodes[Root, 1] = -1 THEN
  4. if Root == -1:
  5. elif ArrayNodes[Root][1] == ValueToFind:
  6. elif ArrayNodes[Root][1] == -1:

Python

def SearchValue(Root, ValueToFind):
                 global ArrayNodes
                 if Root == -1:
                  return -1
                 elif ArrayNodes[Root][1] == ValueToFind:
                   return Root
                 elif ArrayNodes[Root][1] == -1:
                   return -1
                 if(ArrayNodes[Root][1] > ValueToFind):
                   return SearchValue(ArrayNodes[Root][0], ValueToFind)
                 if(ArrayNodes[Root][1] < ValueToFind):
                   return SearchValue(ArrayNodes[Root][2], ValueToFind)

 2022                                  Page 19 of 23

9618/43               Cambridge International AS & A Level – Mark Scheme   October/November
                                          PUBLISHED                                    2022

Mark points

  • IF Root = -1 THEN
  • IF ArrayNodes[Root,1] = ValueToFind THEN
  • IF ArrayNodes[Root, 1] = -1 THEN
  • if Root == -1:
  • elif ArrayNodes[Root][1] == ValueToFind:
  • elif ArrayNodes[Root][1] == -1:

Question 2

Paper page 4 for Q2
Original paper page 4

Part 2(a)(i) — solution

  1. class Card declaration (and end where appropriate)
  2. Private attributes declared Number as integer and Colour as string
  3. constructor header (and end where appropriate)…
  4. …taking 2 parameters
  5. assigning parameters to attributes

Python

class Card:
                   #Number as integer
                   #Colour as string
                   def __init__(self, Number1, Colour1):
                       self.__Number = Number1;
                       self.__Colour = Colour1;

 2022                                  Page 8 of 23

9618/43                  Cambridge International AS & A Level – Mark Scheme       October/November
                                             PUBLISHED                                        2022

Mark points

  • class Card declaration (and end where appropriate)
  • Private attributes declared Number as integer and Colour as string
  • constructor header (and end where appropriate)…
  • …taking 2 parameters
  • assigning parameters to attributes

Part 2(a)(i) — solution

  1. Number = Number1
  2. Colour = Colourp

Visual Basic (VB.NET)

Class Card
                 Private Number As Integer
                 Private Colour As String
                 Sub New(Number1, Colourp)
                   Number = Number1
                   Colour = Colourp
                 End Sub
               End Class

Mark points

  • Number = Number1
  • Colour = Colourp

Part 2(a)(ii) — solution

  1. 1 get method as function (and end where appropriate) with no
  2. …returning the value
  3. 2nd correct get method

Visual Basic (VB.NET)

Function GetNumber()
                 Return Number
               End Function

               Function GetColour()
                 Return Colour
               End Function

 2022                                  Page 9 of 23

9618/43                  Cambridge International AS & A Level – Mark Scheme   October/November
                                             PUBLISHED                                    2022

Python

def GetNumber(self):
                   return self.__Number

               def GetColour(self):
                   return self.__Colour

Mark points

  • 1 get method as function (and end where appropriate) with no
  • …returning the value
  • 2nd correct get method

Part 2(a)(iii) — solution

  1. one card initialised as type Card …
  2. … all 15 cards initialised correctly as type Card

Python

OneRed = Card(1, "red")
               TwoRed = Card(2, "red")
               ThreeRed = Card(3, "red")
               FourRed = Card(4, "red")
               FiveRed = Card(5, "red")

               OneBlue = Card(1, "blue")
               TwoBlue = Card(2, "blue")
               ThreeBlue = Card(3, "blue")
               FourBlue = Card(4, "blue")
               FiveBlue = Card(5, "blue")

               OneYellow = Card(1, "yellow")
               TwoYellow = Card(2, "yellow")
               ThreeYellow = Card(3, "yellow")
               FourYellow = Card(4, "yellow")
               FiveYellow = Card(5, "yellow")

Mark points

  • one card initialised as type Card …
  • … all 15 cards initialised correctly as type Card

Part 2(a)(iii) — solution

VB.NET Dim OneRed As New Card (1, "red") Dim TwoRed As New Card(2, "red") Dim ThreeRed As New Card(3, "red") Dim FourRed As New Card(4, "red") Dim FiveRed As New Card(5, "red") Dim OneBlue As New Card(1, "blue") Dim TwoBlue As New Card(2, "blue") Dim ThreeBlue As New Card(3, "blue") Dim FourBlue As New Card(4, "blue") Dim FiveBlue As New Card(5, "blue") Dim OneYellow As New Card(1, "yellow") Dim TwoYellow As New Card(2, "yellow") Dim ThreeYellow As New Card(3, "yellow") Dim FourYellow As New Card(4, "yellow") Dim FiveYellow As New Card(5, "yellow")

Visual Basic (VB.NET)

Dim OneRed As New Card (1, "red")
               Dim TwoRed As New Card(2, "red")
               Dim ThreeRed As New Card(3, "red")
               Dim FourRed As New Card(4, "red")
               Dim FiveRed As New Card(5, "red")
               Dim OneBlue As New Card(1, "blue")
               Dim TwoBlue As New Card(2, "blue")
               Dim ThreeBlue As New Card(3, "blue")
               Dim FourBlue As New Card(4, "blue")
               Dim FiveBlue As New Card(5, "blue")
               Dim OneYellow As New Card(1, "yellow")
               Dim TwoYellow As New Card(2, "yellow")
               Dim ThreeYellow As New Card(3, "yellow")
               Dim FourYellow As New Card(4, "yellow")
               Dim FiveYellow As New Card(5, "yellow")

Mark points

  • Include the ideas shown in the solution above.

Part 2(b)(i) — solution

  1. class Hand declaration (and end where appropriate)
  2. private attribute declarations; FirstCard as integer, NumberCards as
  3. private attribute array named Cards of type Card with 10 elements
  4. constructor with 5 Card objects as parameters
  5. assigning each Card parameter to the array (in constructor)
  6. initialising FirstCard to 0 and NumberCards to 5 (in constructor)

Python

class Hand:
                   #Cards[10] as Card
                   #FirstCard as integer
                   #NumberCards as integer

                   def __init__(self, Card1, Card2, Card3, Card4,
               Card5):
                       self.__Cards = []
                       self.__Cards.append(Card1)
                       self.__Cards.append(Card2)
                       self.__Cards.append(Card3)
                       self.__Cards.append(Card4)
                       self.__Cards.append(Card5)
                       self.__FirstCard = 0
                       self.__NumberCards = 5

 2022                                 Page 11 of 23

9618/43                 Cambridge International AS & A Level – Mark Scheme     October/November
                                            PUBLISHED                                      2022

Mark points

  • class Hand declaration (and end where appropriate)
  • private attribute declarations; FirstCard as integer, NumberCards as
  • private attribute array named Cards of type Card with 10 elements
  • constructor with 5 Card objects as parameters
  • assigning each Card parameter to the array (in constructor)
  • initialising FirstCard to 0 and NumberCards to 5 (in constructor)

Part 2(b)(i) — solution

  1. Cards(0) = Card1
  2. Cards(1) = Card2
  3. Cards(2) = Card3
  4. Cards(3) = Card4
  5. Cards(4) = Card5
  6. FirstCard = 0
  7. NumberCards = 5

Visual Basic (VB.NET)

class Hand
                 Private Cards(9) As Card
                 Private FirstCard As Integer
                 Private NumberCards As Integer

                 Sub New(Card1, Card2, Card3, Card4, Card5)
                   Cards(0) = Card1
                   Cards(1) = Card2
                   Cards(2) = Card3
                   Cards(3) = Card4
                   Cards(4) = Card5
                   FirstCard = 0
                   NumberCards = 5
                 End Sub
               End Class

Mark points

  • Cards(0) = Card1
  • Cards(1) = Card2
  • Cards(2) = Card3
  • Cards(3) = Card4
  • Cards(4) = Card5
  • FirstCard = 0
  • NumberCards = 5

Part 2(b)(ii) — solution

  1. function GetCard() header (and end where appropriate) taking (integer)
  2. returning the card at parameter index in array

Visual Basic (VB.NET)

Function GetCard(Position)
                 Return Cards(Position)
               End Function

 2022                                 Page 12 of 23

9618/43                  Cambridge International AS & A Level – Mark Scheme           October/November
                                             PUBLISHED                                            2022

Python

def GetCard(self, Position):
                   return self.__Cards[Position]

Mark points

  • function GetCard() header (and end where appropriate) taking (integer)
  • returning the card at parameter index in array

Part 2(b)(iii) — solution

  1. 2 variables (player 1 and player 2) of type Hand
  2. using constructor and sending the correct variables as parameters

Visual Basic (VB.NET)

Dim Player1 As New Hand(OneRed, TwoRed, ThreeRed,
               FourRed, OneYellow)
               Dim Player2 As New Hand(TwoYellow, ThreeYellow,
               FourYellow, FiveYellow, OneBlue)

Python

Player1 = Hand(OneRed, TwoRed, ThreeRed, FourRed,
               OneYellow)
               Player2 = Hand(TwoYellow, ThreeYellow, FourYellow,
               FiveYellow, OneBlue)

Mark points

  • 2 variables (player 1 and player 2) of type Hand
  • using constructor and sending the correct variables as parameters

Part 2(c)(i) — solution

  1. function CalculateValue() header (and end where appropriate)
  2. looping through all 5 Card objects in parameter array…
  3. … adding 5 to score for red, 10 to score for blue, 15 to score if yellow
  4. … adding each card number to score
  5. Using GetCard(), GetColour() and GetNumber() correctly
  6. Returning calculated score

Python

def CalculateValue(Player):
                   Score = 0
                   for Count in range(0, 4):
                       CardGot = Player.GetCard(Count)
                       Score = Score + CardGot.GetNumber()
                       Colour = CardGot.GetColour()
                       if Colour == "red":
                           Score = Score + 5
                       elif Colour == "blue":
                           Score = Score + 10
                       else:
                           Score = Score + 15
                   return Score

 2022                                    Page 13 of 23

9618/43                 Cambridge International AS & A Level – Mark Scheme   October/November
                                            PUBLISHED                                    2022

Mark points

  • function CalculateValue() header (and end where appropriate)
  • looping through all 5 Card objects in parameter array…
  • … adding 5 to score for red, 10 to score for blue, 15 to score if yellow
  • … adding each card number to score
  • Using GetCard(), GetColour() and GetNumber() correctly
  • Returning calculated score

Part 2(c)(i) — solution

  1. Dim Score As Integer = 0
  2. For Count = 0 To 4
  3. CardGot = Player.GetCard(Count)
  4. Score = Score + CardGot.GetNumber()
  5. Colour = CardGot.GetColour()
  6. If Colour = "red" Then
  7. Score = Score + 5
  8. ElseIf Colour = "blue" Then
  9. Score = Score + 10
  10. Score = Score + 15

Visual Basic (VB.NET)

Function CalculateValue(Player As Hand)
                 Dim Score As Integer = 0
                 Dim Colour As String
                 Dim CardGot As Card
                 For Count = 0 To 4
                   CardGot = Player.GetCard(Count)
                   Score = Score + CardGot.GetNumber()
                   Colour = CardGot.GetColour()
                   If Colour = "red" Then
                      Score = Score + 5
                   ElseIf Colour = "blue" Then
                      Score = Score + 10
                   Else
                      Score = Score + 15
                   End If
                 Next
                 Return Score
               End Function

 2022                                Page 14 of 23

9618/43                  Cambridge International AS & A Level – Mark Scheme       October/November
                                             PUBLISHED                                        2022

Mark points

  • Dim Score As Integer = 0
  • For Count = 0 To 4
  • CardGot = Player.GetCard(Count)
  • Score = Score + CardGot.GetNumber()
  • Colour = CardGot.GetColour()
  • If Colour = "red" Then
  • Score = Score + 5
  • ElseIf Colour = "blue" Then
  • Score = Score + 10
  • Score = Score + 15

Part 2(c)(ii) — solution

  1. One function call of CalculateValue( ) for each player …
  2. …sending the player's hand as parameter
  3. Comparing return values and outputting the player with the highest
  4. … or if there was a draw in appropriate message

Visual Basic (VB.NET)

Dim Player1score As Integer
               Dim Player2score As Integer
               Player1score = CalculateValue(Player1)
               Player2score = CalculateValue(Player2)
               If Player1score > Player2score Then
                 Console.WriteLine("Player 1 wins")
               ElseIf Player1score < Player2score Then
                 Console.WriteLine("Player 2 wins")
               Else
                 Console.WriteLine("It's a draw")
               End If

Python

Player1score = CalculateValue(Player1)
               Player2score = CalculateValue(Player2)
               if Player1score > Player2score:
                   print("Player 1 wins")
               elif Player1score < Player2score:
                   print("Player 2 wins")
               else:
                   print("It's a draw")

Mark points

  • One function call of CalculateValue( ) for each player …
  • …sending the player's hand as parameter
  • Comparing return values and outputting the player with the highest
  • … or if there was a draw in appropriate message

Question 3

Paper page 8 for Q3
Original paper page 8

Part 3(a) — solution

  1. Declaring (global) 2D array ArrayNodes
  2. looping through all 20  3 elements of array …
  3. …. storing −1 in each element

Visual Basic (VB.NET)

Dim ArrayNodes(19, 2) As Integer
               Sub main()
                 For X = 0 To 19
                   For Y = 0 To 2
                      ArrayNodes(X, Y) = -1
                   Next
                 Next
               End Sub

 2022                                  Page 16 of 23

9618/43                  Cambridge International AS & A Level – Mark Scheme        October/November
                                             PUBLISHED                                         2022

Python

ArrayNodes = []
               for x in range(0, 20):
                   ArrayNodes.append([-1, -1, -1])

Mark points

  • Declaring (global) 2D array ArrayNodes
  • looping through all 20  3 elements of array …
  • …. storing −1 in each element

Part 3(b) — solution

  1. initialising each of the first 6 array elements correctly
  2. declaring and initialising FreeNode to 6 and RootPointer to 0

Python

ArrayNodes = [[1,20,5],[2,15,-1],[-1,3,3],[-1,9,4],[-
               1,10,-1],[-1,58,-1]]
               FreeNodes = 6
               RootPointer = 0

Mark points

  • initialising each of the first 6 array elements correctly
  • declaring and initialising FreeNode to 6 and RootPointer to 0

Part 3(b) — solution

  1. ArrayNodes(0, 0) = 1
  2. ArrayNodes(0, 1) = 20
  3. ArrayNodes(0, 2) = 5
  4. ArrayNodes(1, 0) = 2
  5. ArrayNodes(1, 1) = 15
  6. ArrayNodes(1, 2) = -1
  7. ArrayNodes(2, 0) = -1
  8. ArrayNodes(2, 1) = 3
  9. ArrayNodes(2, 2) = 3
  10. ArrayNodes(3, 0) = -1
  11. ArrayNodes(3, 1) = 9
  12. ArrayNodes(3, 2) = 4
  13. ArrayNodes(4, 0) = -1
  14. ArrayNodes(4, 1) = 10
  15. ArrayNodes(4, 2) = -1
  16. ArrayNodes(5, 0) = -1
  17. ArrayNodes(5, 1) = 58
  18. ArrayNodes(5, 2) = -1
  19. Dim FreeNode As Integer = 6
  20. Dim RootPointer As Integer = 0

Visual Basic (VB.NET)

ArrayNodes(0, 0) = 1
               ArrayNodes(0, 1) = 20
               ArrayNodes(0, 2) = 5

               ArrayNodes(1, 0) = 2
               ArrayNodes(1, 1) = 15
               ArrayNodes(1, 2) = -1

               ArrayNodes(2, 0) = -1
               ArrayNodes(2, 1) = 3
               ArrayNodes(2, 2) = 3

               ArrayNodes(3, 0) = -1
               ArrayNodes(3, 1) = 9
               ArrayNodes(3, 2) = 4

               ArrayNodes(4, 0) = -1
               ArrayNodes(4, 1) = 10
               ArrayNodes(4, 2) = -1

               ArrayNodes(5, 0) = -1
               ArrayNodes(5, 1) = 58
               ArrayNodes(5, 2) = -1

               Dim FreeNode As Integer = 6
               Dim RootPointer As Integer = 0

 2022                              Page 18 of 23

9618/43                  Cambridge International AS & A Level – Mark Scheme   October/November
                                             PUBLISHED                                    2022

Mark points

  • ArrayNodes(0, 0) = 1
  • ArrayNodes(0, 1) = 20
  • ArrayNodes(0, 2) = 5
  • ArrayNodes(1, 0) = 2
  • ArrayNodes(1, 1) = 15
  • ArrayNodes(1, 2) = -1
  • ArrayNodes(2, 0) = -1
  • ArrayNodes(2, 1) = 3
  • ArrayNodes(2, 2) = 3
  • ArrayNodes(3, 0) = -1
  • ArrayNodes(3, 1) = 9
  • ArrayNodes(3, 2) = 4
  • ArrayNodes(4, 0) = -1
  • ArrayNodes(4, 1) = 10
  • ArrayNodes(4, 2) = -1
  • ArrayNodes(5, 0) = -1
  • ArrayNodes(5, 1) = 58
  • ArrayNodes(5, 2) = -1
  • Dim FreeNode As Integer = 6
  • Dim RootPointer As Integer = 0

Part 3(c) — solution

1 mark for each completed statement (4) 5

Mark points

  • Include the ideas shown in the solution above.

Part 3(c) — solution

  1. If ArrayNodes(Root, 1) = ValueToFind Then
  2. ElseIf ArrayNodes(Root, 1) = -1 Then

Visual Basic (VB.NET)

Function SearchValue(ByVal Root, ByVal ValueToFind)

                 If ArrayNodes(Root, 1) = ValueToFind Then
                   Return Root
                 ElseIf ArrayNodes(Root, 1) = -1 Then
                   Return -1
                 End If
                 If ArrayNodes(Root, 1) > ValueToFind Then
                   Return SearchValue(ArrayNodes(Root, 0), ValueToFind)
                 End If
                 If ArrayNodes(Root, 1) < ValueToFind Then
                   Return SearchValue(ArrayNodes(Root, 2), ValueToFind)
                 End If
                 Return -1
               End Function

 2022                              Page 20 of 23

9618/43                  Cambridge International AS & A Level – Mark Scheme   October/November
                                             PUBLISHED                                    2022

Mark points

  • If ArrayNodes(Root, 1) = ValueToFind Then
  • ElseIf ArrayNodes(Root, 1) = -1 Then

Part 3(d) — solution

  1. (procedure) header (and end where appropriate) with one parameter
  2. checking if left node is −1 …
  3. … if not recursive call with parameter as ArrayNodes[RootNode[0]]
  4. checking if right node is −1 …
  5. …if not recursive call with parameter as ArrayNodes[RootNode[2]]
  6. outputting the element at the parameter RootNode[]
  7. all 3 in the correct order

Visual Basic (VB.NET)

Sub PostOrder(RootNode() As Integer)
                   Dim TempArray(2) As Integer
                   If RootNode(0) <> -1 Then
                       TempArray(0) = ArrayNodes(RootNode(0), 0)
                       TempArray(1) = ArrayNodes(RootNode(0), 1)
                       TempArray(2) = ArrayNodes(RootNode(0), 2)
                       PostOrder(TempArray)
                   End If
                   If RootNode(2) <> -1 Then
                       TempArray(0) = ArrayNodes(RootNode(2), 0)
                       TempArray(1) = ArrayNodes(RootNode(2), 1)
                       TempArray(2) = ArrayNodes(RootNode(2), 2)
                       PostOrder(TempArray)
                   End If
                   Console.WriteLine(RootNode(1))
               End Sub

 2022                                 Page 21 of 23

9618/43                  Cambridge International AS & A Level – Mark Scheme   October/November
                                             PUBLISHED                                    2022

Python

def PostOrder(RootNode):
                   if RootNode[0] != -1:
                       PostOrder(ArrayNodes[RootNode[0]])
                   if RootNode[2] != -1:
                       PostOrder(ArrayNodes[RootNode[2]])
                   print(str(RootNode[1]))

Mark points

  • (procedure) header (and end where appropriate) with one parameter
  • checking if left node is −1 …
  • … if not recursive call with parameter as ArrayNodes[RootNode[0]]
  • checking if right node is −1 …
  • …if not recursive call with parameter as ArrayNodes[RootNode[2]]
  • outputting the element at the parameter RootNode[]
  • all 3 in the correct order

Part 3(e)(i) — solution

  1. calling SearchValue() with 15 and rootPointer as a parameter …
  2. … if return value > -1 output returned index and
  3. Calling PostOrder() with ArrayNodes[RootPointer] as a

Visual Basic (VB.NET)

Dim returnvalue As Integer = SearchValue(RootPointer, 15)
               If returnvalue = -1 Then
                    Console.WriteLine("Not found")
               Else
                    Console.WriteLine("Found at " & returnvalue)
               End If
               Console.WriteLine("Post order")
               Dim TempArray(2) As Integer
               TempArray(0) = ArrayNodes(RootPointer, 0)
               TempArray(1) = ArrayNodes(RootPointer, 1)
               TempArray(2) = ArrayNodes(RootPointer, 2)
               PostOrder(TempArray)

 2022                                 Page 22 of 23

9618/43                  Cambridge International AS & A Level – Mark Scheme   October/November
                                             PUBLISHED                                    2022

Python

ReturnValue = SearchValue(RootPointer, 15)
               if ReturnValue == -1:
                   print("Not found")
               else:
                   print("Found at " + str(ReturnValue))
               PostOrder(ArrayNodes[RootPointer])

Mark points

  • calling SearchValue() with 15 and rootPointer as a parameter …
  • … if return value > -1 output returned index and
  • Calling PostOrder() with ArrayNodes[RootPointer] as a

Part 3(e)(ii) — solution

Screenshot with result as shown, for example: 1 2022 Page 23 of 23

Mark points

  • Include the ideas shown in the solution above.