9618/42 · 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) 2-D array Jobs with correct identifier (and Integer data type)
  2. … with 100 elements by 2 elements
  3. (global) NumberOfJobs declared as variable (as Integer)

Visual Basic (VB.NET)

Dim Jobs(99, 1) As Integer
               Dim NumberOfJobs As Integer

 2022                                    Page 3 of 24

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

Python

Jobs # global integer, 100 by 2 elements
               NumberOfJobs # global integer

Mark points

  • (global) 2-D array Jobs with correct identifier (and Integer data type)
  • … with 100 elements by 2 elements
  • (global) NumberOfJobs declared as variable (as Integer)

Part 1(b) — solution

  1. procedure heading (and end where appropriate) and assigns 0 to
  2. looping through both array element dimensions
  3. … assigns −1 to all elements

Visual Basic (VB.NET)

Sub Initialise()
                 For X = 0 To 99
                   For Y = 0 To 1
                      Jobs(X, Y) = -1
                   Next
                 Next
                 NumberOfJobs = 0
               End Sub

 2022                                  Page 4 of 24

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

Python

def Initialise():
                   global Jobs
                   global NumberOfJobs
                   for x in range(0, 100):
                       Jobs.append([-1,-1])
                   NumberOfJobs = 0

Mark points

  • procedure heading (and end where appropriate) and assigns 0 to
  • looping through both array element dimensions
  • … assigns −1 to all elements

Part 1(c) — solution

  1. Function header (and end where appropriate) with two (integer)
  2. Checks if array is full …
  3. … if full outputs "Not added"
  4. Storing parameters job number and priority to only the next available
  5. Incrementing NumberOfJobs
  6. Outputting "Added" if successful

Visual Basic (VB.NET)

Sub AddJob(JobNumber, Priority)
                 If NumberOfJobs = 100 Then
                   Console.WriteLine("Not added")
                 Else
                   Jobs(NumberOfJobs, 0) = JobNumber
                   Jobs(NumberOfJobs, 1) = Priority
                   NumberOfJobs = NumberOfJobs + 1
                   Console.WriteLine("Added")
                 End If
               End Sub

 2022                                   Page 5 of 24

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

Python

def AddJob(JobNumber, Priority):
                 global NumberOfJobs
                 global Jobs
                 if NumberOfJobs == 100:
                   print("Not added")
                 else:
                   Jobs[NumberOfJobs] = [JobNumber, Priority]
                   print("Added")
                   NumberOfJobs = NumberOfJobs + 1

Mark points

  • Function header (and end where appropriate) with two (integer)
  • Checks if array is full …
  • … if full outputs "Not added"
  • Storing parameters job number and priority to only the next available
  • Incrementing NumberOfJobs
  • Outputting "Added" if successful

Part 1(d) — solution

  1. Calls Initialise() (in the main program)
  2. 5 AddJob calls with correct values as parameters in correct order

Visual Basic (VB.NET)

Sub Main()
                 Initialise()
                 AddJob(12, 10)
                 AddJob(526, 9)
                 AddJob(33, 8)
                 AddJob(12, 9)
                 AddJob(78, 1)
               End Sub

 2022                                   Page 6 of 24

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

Python

Initialise()
               AddJob(12,10)
               AddJob(526,9)
               AddJob(33,8)
               AddJob(12,9)
               AddJob(78,1)

Mark points

  • Calls Initialise() (in the main program)
  • 5 AddJob calls with correct values as parameters in correct order

Part 1(e) — solution

  1. Procedure header (and end where appropriate)
  2. Outer loop through all 5 elements / number of jobs …
  3. … inner loop through array elements …
  4. …and comparing priority (second index) …
  5. …moving the elements up and inserting correctly

Python

def InsertionSort():
                   global Jobs
                   global NumberOfJobs
                   for I in range(1, NumberOfJobs):
                       Current1 = Jobs[I][0]
                       Current2 = Jobs[I][1]
                       while I > 0 and Jobs[I-1][1] > Current2:
                           Jobs[I][0] = Jobs[I-1][0]
                           Jobs[I][1] = Jobs[I-1][1]
                           I = I - 1
                       Jobs[I][0] = Current1
                       Jobs[I][1] = Current2

Mark points

  • Procedure header (and end where appropriate)
  • Outer loop through all 5 elements / number of jobs …
  • … inner loop through array elements …
  • …and comparing priority (second index) …
  • …moving the elements up and inserting correctly

Part 1(e) — solution

  1. For i = 1 To NumberOfJobs - 1
  2. Tempa = Jobs(i, 0)
  3. Tempb = Jobs(i, 1)
  4. Counter = i
  5. Placed = False
  6. Jobs(Counter, 0) = Jobs(Counter - 1, 0)
  7. Jobs(Counter, 1) = Jobs(Counter - 1, 1)
  8. Counter = Counter - 1
  9. Placed = True
  10. Jobs(Counter, 0) = Tempa
  11. Jobs(Counter, 1) = Tempb

Visual Basic (VB.NET)

Sub InsertionSort()
                  Dim Tempa As Integer
                  Dim Tempb As Integer
                  Dim Counter As Integer
                  Dim Placed As Boolean
                  For i = 1 To NumberOfJobs - 1
                       Tempa = Jobs(i, 0)
                       Tempb = Jobs(i, 1)
                       Counter = i
                       Placed = False
                       While (Counter > 0 And Not Placed)
                           If (Jobs(Counter - 1, 1) > Tempb) Then
                                Jobs(Counter, 0) = Jobs(Counter - 1, 0)
                                Jobs(Counter, 1) = Jobs(Counter - 1, 1)
                                Counter = Counter - 1
                           Else
                                Placed = True
                           End If
                       End While
                       Jobs(Counter, 0) = Tempa
                       Jobs(Counter, 1) = Tempb
                  Next i
               End Sub

 2022                              Page 8 of 24

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

Mark points

  • For i = 1 To NumberOfJobs - 1
  • Tempa = Jobs(i, 0)
  • Tempb = Jobs(i, 1)
  • Counter = i
  • Placed = False
  • Jobs(Counter, 0) = Jobs(Counter - 1, 0)
  • Jobs(Counter, 1) = Jobs(Counter - 1, 1)
  • Counter = Counter - 1
  • Placed = True
  • Jobs(Counter, 0) = Tempa
  • Jobs(Counter, 1) = Tempb

Part 1(f) — solution

  1. procedure heading (and end where appropriate) and outputting all job
  2. Outputting the job and priority for each element on the same line, with a
  3. … with 'priority' between job number and priority

Visual Basic (VB.NET)

Sub PrintArray()
                 For X = 0 To NumberOfJobs - 1
                   Console.WriteLine(Jobs(X, 0) & " priority " & Jobs(X,
               1))
                 Next
               End Sub

Python

def PrintArray():
                 global Jobs
                 global NumberOfJobs
                 for X in range(0, NumberOfJobs):
                   print(str(Jobs[X][0]), " priority ", str(Jobs[X][1]))

Mark points

  • procedure heading (and end where appropriate) and outputting all job
  • Outputting the job and priority for each element on the same line, with a
  • … with 'priority' between job number and priority

Part 1(g)(i) — solution

  1. calling both subroutines in the main program in the correct order 1

Visual Basic (VB.NET)

InsertionSort()
               PrintArray()

 2022                                    Page 9 of 24

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

Python

InsertionSort()
               PrintArray()

Mark points

  • calling both subroutines in the main program in the correct order 1

Part 1(g)(ii) — solution

1 mark for added 5 times and jobs in order. 1 526 and 12 can be reversed 2022 Page 10 of 24 9618/42 Cambridge International AS & A Level – Mark Scheme October/November PUBLISHED 2022

Mark points

  • Include the ideas shown in the solution above.

Question 2

Paper page 5 for Q2
Original paper page 5

Part 2(a) — solution

  1. Class declaration (and end where appropriate) for Character
  2. Declaring the 3 private attributes with appropriate data types; Name as
  3. Constructor method (and end where appropriate) taking 3 parameters …
  4. …assigning parameters to all 3 attributes

Visual Basic (VB.NET)

Class Character
                 Private Name As String
                 Private XCoordinate As Integer
                 Private YCoordinate As Integer
                 Sub New(Namep, Xcoord, Ycoord)
                   Name = Namep
                   XCoordinate = Xcoord
                   YCoordinate = Ycoord
                 End Sub
               End Class

 2022                                  Page 11 of 24

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

Python

class Character:
                   #private Name as string
                   #private XCoordinate as integer
                   #private YCoordinate as integer
                   def __init__(self, Namep, Xcoord, Ycoord):
                       self.__Name = Namep
                       self.__XCoordiante = Xcoord
                       self.__YCoordinate = Ycoord

Mark points

  • Class declaration (and end where appropriate) for Character
  • Declaring the 3 private attributes with appropriate data types; Name as
  • Constructor method (and end where appropriate) taking 3 parameters …
  • …assigning parameters to all 3 attributes

Part 2(b) — solution

  1. 1 get method header (and end where appropriate) with no parameters…
  2. …returning correct value
  3. 2nd and 3rd correct get methods

Visual Basic (VB.NET)

Function GetName()
                 Return Name
               End Function
               Function GetX()
                 Return XCoordinate
               End Function
               Function GetY()
                 Return YCoordinate
               End Function

 2022                                 Page 12 of 24

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

Python

def GetName(self):
                       return self.__Name
                   def GetX(self):
                       return self.__XCoordinate
                   def GetY(self):
                       return self.__YCoordinate

Mark points

  • 1 get method header (and end where appropriate) with no parameters…
  • …returning correct value
  • 2nd and 3rd correct get methods

Part 2(c) — solution

  1. method header (and end where appropriate) taking 2 (integer)
  2. adding both parameters to existing x and y coordinate values

Visual Basic (VB.NET)

Sub changePosition(XChange, YChange)
                 XCoordinate = XCoordinate + XChange
                 YCoordinate = YCoordinate + YChange
               End Sub

 2022                                  Page 13 of 24

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

Python

def ChangePosition(self, XChange, YChange):
                 self.__XCoordinate = self.__XCoordinate + XChange
                 self.__YCoordinate = self.__YCoordinate + YChange

Mark points

  • method header (and end where appropriate) taking 2 (integer)
  • adding both parameters to existing x and y coordinate values

Part 2(d) — solution

  1. declaration of 1D array, 10 elements of type Character
  2. opening text file Characters.txt to read
  3. looping until EOF/10 times…
  4. … reading in each 3-set of values from file …
  5. … instantiate a Character with correct parameters read in from file…
  6. … store in next element/append in declared array
  7. closing the text file (in appropriate place)
  8. Exception handling for opening and reading data from file…
  9. … with appropriate catch and output

Python

Characters = []
               TextFile = "Characters.txt"
               try:
                 File = open(TextFile, 'r')
                 for X in range(0, 10):
                      Name = File.readline().strip()
                      XCoord = File.readline().strip()
                      YCoord = File.readline().strip()
                      TempC = Character(Name, int(XCoord), int(YCoord))
                      Characters.append(TempC)
                 File.close()
               except:
                 print("File not found")

 2022                                 Page 14 of 24

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

Mark points

  • declaration of 1D array, 10 elements of type Character
  • opening text file Characters.txt to read
  • looping until EOF/10 times…
  • … reading in each 3-set of values from file …
  • … instantiate a Character with correct parameters read in from file…
  • … store in next element/append in declared array
  • closing the text file (in appropriate place)
  • Exception handling for opening and reading data from file…
  • … with appropriate catch and output

Part 2(d) — solution

  1. Dim TextFile As String = "Characters.txt"
  2. For X = 0 To 10
  3. Name = FileReader.ReadLine()
  4. Xcoord = FileReader.ReadLine()
  5. Ycoord = FileReader.ReadLine()
  6. Characters(X) = New Character(Name, Xcoord, Ycoord)

Visual Basic (VB.NET)

Sub Main()
                 Dim Characters(0 To 9) As Character
                 Dim TextFile As String = "Characters.txt"
                 Try
                   Dim FileReader As New
               System.IO.StreamReader(TextFile)
                   For X = 0 To 10
                     Name = FileReader.ReadLine()
                     Xcoord = FileReader.ReadLine()
                     Ycoord = FileReader.ReadLine()
                     Characters(X) = New Character(Name, Xcoord, Ycoord)
                   Next
                   FileReader.Close()
                 Catch ex As Exception
                   Console.WriteLine("File not found")
                 End Try
               end sub

 2022                              Page 15 of 24

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

Mark points

  • Dim TextFile As String = "Characters.txt"
  • For X = 0 To 10
  • Name = FileReader.ReadLine()
  • Xcoord = FileReader.ReadLine()
  • Ycoord = FileReader.ReadLine()
  • Characters(X) = New Character(Name, Xcoord, Ycoord)

Part 2(e) — solution

  1. Taking name as input …
  2. …converting/checking case e.g. all to lower
  3. Looping through array of characters comparing each character name to
  4. …continuously taking repeat input if not found in array
  5. …storing the index when found
  6. Accessing the name of character in the array using GetName()

Visual Basic (VB.NET)

Dim Position As Integer = -1
               Dim CharacterName As String = ""
               While Position = -1
                 Console.WriteLine("Enter the Character to move")
                 CharacterName = Console.ReadLine
                 For Count = 0 To 9
                   If(Characters(Count).GetName).tolower =
               CharacterName.ToLower Then
                      Position = Count
                   End If
                 Next
               End While

 2022                                  Page 16 of 24

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

Python

Position = -1
               CharacterName = ""
               while(Position == -1):
                 CharacterInput = input("Enter the Character to
               move").rstrip('\n').lower()
                 for Count in range(0, 10):
                   Temp = str(Characters[Count].GetName().strip())
                   if(Temp == CharacterInput):
                      Position = Count

Mark points

  • Taking name as input …
  • …converting/checking case e.g. all to lower
  • Looping through array of characters comparing each character name to
  • …continuously taking repeat input if not found in array
  • …storing the index when found
  • Accessing the name of character in the array using GetName()

Part 2(e) — solution

Mark points

  • Include the ideas shown in the solution above.

Part 2(f) — solution

  1. Taking move as input…
  2. …looping until valid
  3. Calling ChangePosition()with object
  4. If A is input parameters are −1, 0
  5. If D is input parameters are 1, 0
  6. If W is input parameters are 0, 1
  7. If S is input parameters are 0, −1

Python

IsValid = False
               while(IsValid != True):
                   Move = input("Enter A for left, W for up, S for down,
               or D for right")
                   if(Move.upper() == "A"):
                       Characters[Position].ChangePosition(-1,0)
                       IsValid = True
                   elif (Move.upper() == "W"):
                       Characters[Position].ChangePosition(0,1)
                       IsValid = True
                   elif (Move.upper() == "S"):
                       Characters[Position].ChangePosition(0,-1)
                       IsValid = True
                   elif(Move.upper() == "D"):
                       Characters[Position].ChangePosition(1,0)
                       IsValid = True

 2022                                 Page 18 of 24

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

Mark points

  • Taking move as input…
  • …looping until valid
  • Calling ChangePosition()with object
  • If A is input parameters are −1, 0
  • If D is input parameters are 1, 0
  • If W is input parameters are 0, 1
  • If S is input parameters are 0, −1

Part 2(f) — solution

  1. Dim IsValid As Boolean = False
  2. Move = Console.ReadLine()
  3. If Move.ToUpper = "A" Then
  4. IsValid = True
  5. ElseIf Move.ToUpper = "W" Then
  6. IsValid = True
  7. ElseIf Move.ToUpper = "S" Then
  8. IsValid = True
  9. ElseIf Move.ToUpper = "D" Then
  10. IsValid = True

Visual Basic (VB.NET)

Dim IsValid As Boolean = False
               Dim Move As String
               While IsValid <> True
                 Console.WriteLine("Enter A for left, W for up, S for
               down or D for right")
                 Move = Console.ReadLine()
                 If Move.ToUpper = "A" Then
                   Characters(Position).ChangePosition(-1, 0)
                   IsValid = True
                 ElseIf Move.ToUpper = "W" Then
                   Characters(Position).ChangePosition(0, 1)
                   IsValid = True
                 ElseIf Move.ToUpper = "S" Then
                   Characters(Position).ChangePosition(0, -1)
                   IsValid = True
                 ElseIf Move.ToUpper = "D" Then
                   Characters(Position).ChangePosition(1, 0)
                   IsValid = True
                 End If
               End While

Mark points

  • Dim IsValid As Boolean = False
  • Move = Console.ReadLine()
  • If Move.ToUpper = "A" Then
  • IsValid = True
  • ElseIf Move.ToUpper = "W" Then
  • IsValid = True
  • ElseIf Move.ToUpper = "S" Then
  • IsValid = True
  • ElseIf Move.ToUpper = "D" Then
  • IsValid = True

Part 2(g)(i) — solution

  1. Outputting given message including name, x and y position
  2. …all using appropriate get methods

Visual Basic (VB.NET)

Console.WriteLine(CharacterName & " has changed
               coordinates to X = " & Characters(Position).GetX & " Y =
               " & Characters(Position).GetY())

 2022                                  Page 19 of 24

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

Python

print(CharacterName, " has changed coordinate to X = ",
               str(Characters[Position].GetX()), " Y = ",
               str(Characters[Position].GetY()))

Mark points

  • Outputting given message including name, x and y position
  • …all using appropriate get methods

Part 2(g)(ii) — solution

1 mark for correct result, for example: 1

Mark points

  • Include the ideas shown in the solution above.

Question 3

Paper page 8 for Q3
Original paper page 8

Part 3(a) — solution

  1. 1D array with 100 (Integer) spaces
  2. head pointer declared initialised to appropriate value e.g. −1
  3. tail pointer declared initialised to 0

Visual Basic (VB.NET)

Dim Queue(0 To 99) As Integer
               Dim HeadPointer As Integer = -1
               Dim TailPointer As Integer = 0

 2022                                    Page 20 of 24

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

Python

Queue = [-1 for I in range(100)] #Integer
               HeadPointer = -1
               TailPointer = 0

Mark points

  • 1D array with 100 (Integer) spaces
  • head pointer declared initialised to appropriate value e.g. −1
  • tail pointer declared initialised to 0

Part 3(b) — solution

  1. Function header (and close where appropriate) with integer parameter
  2. Checking if queue full and returning false
  3. If not full adding parameter to queue at tail pointer …
  4. … incrementing tail pointer (after adding to queue)
  5. … and returning true
  6. Changing head pointer to 0 if this is the first element in array

Visual Basic (VB.NET)

Function Enqueue(Data)
                 If TailPointer < 100 Then
                   If HeadPointer = -1 Then
                     HeadPointer = 0
                   End If
                   Queue(TailPointer) = data
                   TailPointer = TailPointer + 1
                   Return True
                 End If
                 Return False
               End Function

 2022                                  Page 21 of 24

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

Python

def Enqueue(Data):
                    global Queue
                    global TailPointer
                    if(TailPointer < 100):
                      if HeadPointer == -1:
                        HeadPointer = 0
                      Queue[TailPointer] = Data
                      TailPointer = TailPointer + 1
                      return True
                    return False

Mark points

  • Function header (and close where appropriate) with integer parameter
  • Checking if queue full and returning false
  • If not full adding parameter to queue at tail pointer …
  • … incrementing tail pointer (after adding to queue)
  • … and returning true
  • Changing head pointer to 0 if this is the first element in array

Part 3(c) — solution

  1. Looping 20 times
  2. … using Enqueue() with each number 1 to 20 in ascending numerical
  3. … and storing/using the return value
  4. … based on return value, outputting "Successful" and "Unsuccessful" if

Visual Basic (VB.NET)

Dim Success As Boolean
               For Count = 1 To 20
                 Success = Enqueue(Count)
               Next
               If Success = False THEN
                 Console.WriteLine("Unsuccessful")
               ELSE
                 Console.WriteLine("Successful")
               ENDIF

 2022                                  Page 22 of 24

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

Python

Success = False
               for Count in range(1, 21):
                    Success = Enqueue(Count)
               if(Success == False):
                 print("Unsuccessful")
               else:
                 print("Successful")

Mark points

  • Looping 20 times
  • … using Enqueue() with each number 1 to 20 in ascending numerical
  • … and storing/using the return value
  • … based on return value, outputting "Successful" and "Unsuccessful" if

Part 3(d) — solution

  1. function call (and end where appropriate) taking a parameter
  2. checking if at start of queue//20 …
  3. …returning the last value in the queue
  4. (otherwise) adding return value to a total // adding value in queue before
  5. recursive call with Start/pointer −1
  6. returning the final total

Visual Basic (VB.NET)

Function RecursiveOutput(ByVal Start)
                 If (Start = 0) Then
                   Return Queue(Start)
                 Else
                    Return Queue(Start) + RecursiveOutput(Start - 1)
                 End If
               End Function

Python

def RecursiveOutput(Start):
                   if(Start == 0):
                       return Queue[Start]
                   else:
                         return Queue[Start] + RecursiveOutput(Start - 1)

Mark points

  • function call (and end where appropriate) taking a parameter
  • checking if at start of queue//20 …
  • …returning the last value in the queue
  • (otherwise) adding return value to a total // adding value in queue before
  • recursive call with Start/pointer −1
  • returning the final total

Part 3(e)(i) — solution

1 mark for calling function and outputting return value. 1 Example program code: Python print(str(RecursiveOutput(TailPointer - 1))) VB.NET Console.WriteLine(RecursiveOutput(TailPointer - 1)) 2022 Page 23 of 24 9618/42 Cambridge International AS & A Level – Mark Scheme October/November PUBLISHED 2022

Visual Basic (VB.NET)

Console.WriteLine(RecursiveOutput(TailPointer - 1))

 2022                                    Page 23 of 24

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

Python

print(str(RecursiveOutput(TailPointer - 1)))

Mark points

  • Include the ideas shown in the solution above.

Part 3(e)(ii) — solution

1 mark for screenshot showing 210, for example: 1 2022 Page 24 of 24

Mark points

  • Include the ideas shown in the solution above.