Sei sulla pagina 1di 20

CIE O Level

November 2018 – Paper 2


Solutions for Pre-Release Material

©Muzzammil Muttur – October 2018


Please feel free to distribute. No modifications allowed to this document without prior permission.
Email all your suggestions, comments and feedback.
©M. Muttur

Pre Release Material

2
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur

3
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur

Processing Required

4
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur

5
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur

Task 1
 Declare the different arrays to be needed
 Initialise all arrays
 Allow user to enter the names and age of 20 runners
o Do not allow ages below 4 or above 14
o Generate a unique runner ID for each runner
o The fourth digit of the runner id is a check digit

Task 2
 Take the runner ID as input
 Allow user to enter start time and finish time
 Calculate the difference between start time and finish time in seconds
 Convert the time into minutes up to 2 decimal places

Task 3
 Look for the runner ID in the runnerID array. Store the position in the array as runnerIDPosition
o If the runnerID input has not been found in the array, then display a message
 Check the personal best time for this runner
 If the newly run time is better than the recorded personal best, then update the personal best time
o If the personal best time is 0, then no comparison is required. Simply store the newly run time as
personal best time
 Update the number of runs
 If runner has run more than 11 times but less than 22, then assign this runner a wrist band of “Half”
o If number of runs is 22 or more, then the wrist band assigned is 22.
 Look for the fastest runner in each age range
o Pass through the runner age array and consider each runner in the age range 4 to 6. Then find
what is the fast time and what is the position (index) of this fastest runner
o Repeat the same process for age ranges 7 to 10 and also for 11 to 14.
 Output all required data
o Output the names of all runners and their wrist band
o Display the names and times of the fastest runners for the three age categories (4 to 6, 7 to 10 and
11 to 14)

6
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur

Pseudocode Solutions

7
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur

Task 1
Create arrays to store the different data items:
 Runner ID (String as there may be an ID like 0257)
 RunnerName (String)
 RunnerAge (Integer)
 PB (Real) to store the personal best times of each runner
 NumberOfRuns (integer)
 WristBand(Char)
All arrays contain 20 elements as the question requires us to store data for 20 children.

The following pseudocode shows how to declare the above arrays, initialize them and also record their personal
best times. The number of runs is also updated each time a child completes a run event.

DECLARE RunnerID:ARRAY[1..20]:STRING
DECLARE RunnerName:ARRAY[1..20]:STRING
DECLARE RunnerAge:ARRAY[1..20]:INTEGER
DECLARE PB:ARRAY[1..20]:REAL
DECLARE NumberOfRuns:ARRAY[1..20]:INTEGER
DECLARE WristBand:ARRAY[1..20]:CHAR

FOR i  1 to 20
RunnerID[i] “”
RunnerName[i]  “”
RunnerAge [i]  0
PB[i]  0
NumberOfRuns[i] 0
WristBand  “-“
NEXT i

//inputting children details (name, age and generating the ID)


FOR i  1 to 20
INPUT Name
RunnerName[i] Name

REPEAT
INPUT Age
RunnerAge [i] Age
IF Age < 4 OR Age > 14 then
OUTPUT “Please enter a value between 4 and 14”
END IF
UNTIL Age>=4 AND Age<=14

8
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur
//check that generated ID is unique. Otherwise generate another RunnerID
REPEAT
currentID = generateRunnerID()

RunnerIDFound  FALSE
FOR j 1 to 20
If RunnerID[j] = currentID then
RunnerIDFound  TRUE
NEXT j
UNTIL RunnerIDFound = FALSE
RunnerID(i)  currentRunnerID

NEXT i

FUNCTION generateRunnerID()
DECLARE a,b,c,d as INTEGER
REPEAT
//assign a, b and c random numbers between 0 and 9
a  random() * 10
b  random() * 10
c  random() * 10
//d is calculated by multiplying each digit by a weight assigned to it
//the sum of all products is then divided by 11 and the remainder is taken
//the remainder is subtracted from 11 and the result is the check digit

d 11-(((a*3)+(b*2)+(c*1))MOD11)

//d should be a single digit. If d happens to be 11 or 10, then another RunnerID is generated
UNTIL d < 10
END FUNCTION

9
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur

Task 2
//record run times for each runner and then update the number of runs
INPUT currentRunnerID
INPUT startTime
INPUT finishTime
//calculate the number of seconds between the start time and finish times
runDurationSeconds  DateDiff(Seconds, startTime, finishTime)

//divide the number of seconds run by 60 to get the number of minutes run
runDurationMinutes  runDurationSeconds / 60

//store the run duration in minutes with only two decimal places
runDurationMinutes  Round(runDurationMinutes, 2)

Task 3
//find the position of this runner id in the RunnerID array
runnerFound  False

For i  1 TO 20
If RunnerID(i) = currentRunnerID Then
runnerIDPosition  i
runnerFound  True
End If
Next

//if the runnerID input has not been found in the RunnerID aray
IF runnerFound = False THEN
OUTPUT ("This runner id was not found in the list. Please try again")
ELSE
//if no personal best time has been recorded yet
IF PB[runnerIDPosition]  0 Then
PB[runnerIDPosition] runDurationMinutes
//if the new run time is better than the personal best time
ELSEIF runDurationMinutes < PB[runnerIDPosition] THEN
PB[runnerIDPosition] runDurationMinutes
END IF

//update number of runs


NumberOfRuns[runnerIDPosition] = NumberOfRuns[runnerIDPosition] + 1

//H is for half marathon and F is for full marathon


IF NumberOfRuns[runnerIDPosition] > 11 AND NumberOfRuns[runnerIDPosition] < 22 10
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur
WristBand(runnerIDPosition) = "H"
ELSE IF NumberOfRuns(runnerIDPosition) >= 22 THEN
WristBand(runnerIDPosition) = "F"
END IF
END IF

//to find the fastest child


FOR i  1 to 20
IF RunnerAge[i] >= 4 AND RunnerAge[i] <=6 THEN
If PB[i] < fastestTime4To6 THEN
fastestTime4To6  PB[i]
fastestPosition4To6 i
END IF
ELSE IF RunnerAge[i] >= 7 AND RunnerAge[i] <=10 THEN
If PB[i] < fastestTime7To10 THEN
fastestTime7To10  PB[i]
fastestPosition7To10 i
END IF

ELSE IF RunnerAge[i] >= 11 AND RunnerAge[i] <=14 THEN


If PB[i] < fastestTime7To10 THEN
fastestTime7To10  PB[i]
fastestPosition7To10 i
END IF
END IF
NEXT i

//outputting data
//output names of all children and their wrist band (- for no specific wrist band, H for half marathon and F for full
marathon.
FOR i  1 to 20
OUTPUT (“Runner Name is: “ & runnerName[i])
OUTPUT (“Wrist band is: “ & wristBand[i])

NEXT i

//Output name and times of fastest children in the different age ranges
OUTPUT (“Fastest child in the age range 4 to 6: “ & RunnerName[fastestPosition4To6])
OUTPUT (“Fastest time: “ & fastestTime4To6)

OUTPUT (“Fastest child in the age range 7 to 10: “ & RunnerName[fastestPosition7To10])


OUTPUT (“Fastest time: “ & fastestTime7To10)

OUTPUT (“Fastest child in the age range 11 to 14: “ & RunnerName[fastestPosition11To14]) 11


5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur
OUTPUT (“Fastest time: “ & fastestTime11To14)

12
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur

Potential Exam Questions


Based on Past CIE Exam Papers

13
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur

1 Write pseudocode for declaring the array to store the runner name and age.

DECLARE RunnerName:ARRAY[1..20]:STRING
DECLARE RunnerAge:ARRAY[1..20]:INTEGER

2 List three variables in your solution and what they are used for.
Age
To record runner age of runner

i
use to control the loop

runDurationMinutes
used to calculate the run duration from seconds to minutes

3 Give three examples of test data for the runner age.


Normal data: 10
Abnormal data: four, #

4 Show any validation check you have used in your solution


REPEAT
INPUT Age
RunnerAge [i] Age
IF Age < 4 OR Age > 14 then
OUTPUT “Please enter a value between 4 and 14”
END IF
UNTIL Age>=4 AND Age<=14

5 Describe the data structure used in Task 1 to store the runner ID.
An array is used (RunnerID). The data type of the array elements are String.

6 Explain how your program produces a unique runner ID for each runner.
Three digits are generated randomly (digits a, b and c).
These digits are then used to calculate a check digit d (using modulus 11).

The 4 digit runner ID generated is then searched in the RunnerID array. If this runner ID is found in the
array, this means that this number has already been generated and is therefore not unique. The process
is repeated again.

7 Explain how your program for Task 3 updates the children’s data and identify the fastest
child for each age range.
Any programming statements used in your answer must be fully explained.

FOR i  1 to 20 14
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur
IF RunnerAge[i] >= 4 AND RunnerAge[i] <=6 THEN
If PB[i] < fastestTime4To6 THEN
fastestTime4To6  PB[i]
fastestPosition4To6 i
END IF
ELSE IF RunnerAge[i] >= 7 AND RunnerAge[i] <=10 THEN
If PB[i] < fastestTime7To10 THEN
fastestTime7To10  PB[i]
fastestPosition7To10 i
END IF

ELSE IF RunnerAge[i] >= 11 AND RunnerAge[i] <=14 THEN


If PB[i] < fastestTime7To10 THEN
fastestTime7To10  PB[i]
fastestPosition7To10 i
END IF
END IF
NEXT i

8 Comment on the efficiency of your design for Task 3.


Task 3 uses results from tasks 1 and 2
Values entered and generated in Tasks 1 and 2 have already been validated
Algorithm first check if runner ID provided is found in the array before proceeding with any further
processing.

15
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur

Program Code
Microsoft Visual Basic Console Mode

16
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur
Module Module1

Sub Main()
Dim RunnerID(19) As String
Dim RunnerName(19) As String
Dim RunnerAge(19) As Integer
Dim PB(19) As Single
Dim NumberOfRuns(19) As Integer
Dim WristBand(19) As Char

Dim i, j As Integer

Dim currentRunnerID As String


Dim runnerIDFound As Boolean
Dim runnerFound As Boolean
Dim runnerIDPosition As Integer
Dim startTime, finishTime As DateTime

Dim runDurationSeconds As Single


Dim runDurationMinutes As Single
Dim fastestTime4To6, fastestTime7To10, fastestTime11To14 As Single
Dim fastestPosition4To6, fastestPosition7To10, fastestPosition11To14 As Integer

fastestTime4To6 = 9999
fastestTime7To10 = 9999
fastestTime11To14 = 9999
currentRunnerID = 0

Dim testString As String = ""

'registration details (ID, Name and Age) of all 20 runners


For i = 0 To 19
Console.WriteLine("Enter name for runner " & i + 1 & ": ")
RunnerName(i) = Console.ReadLine

Do
Console.WriteLine("Enter age for runner " & i + 1 & ": ")
RunnerAge(i) = Console.ReadLine
If RunnerAge(i) < 4 Or RunnerAge(i) > 14 Then
Console.WriteLine("Please enter a value between 4 and 14.")
End If
Loop Until RunnerAge(i) >= 4 And RunnerAge(i) <= 14

'generate runnerID for the current runner being registered


Do
runnerIDFound = False

currentRunnerID = generateRunnerID()
For j = 0 To 2
If RunnerID(j) = currentRunnerID Then
runnerIDFound = True
End If
Next
Loop Until runnerIDFound = False 17
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur
RunnerID(i) = currentRunnerID
Next

'initialise arrays for PB and number of runs


For i = 0 To 2
PB(i) = 0
NumberOfRuns(i) = 0
WristBand(i) = "-"
Next

runnerFound = False
'recording runs timings and updating PB if needed
While currentRunnerID <> "-1"
Console.WriteLine("Please enter the runner ID: ")
currentRunnerID = Console.ReadLine

If currentRunnerID <> "-1" Then


'get start and fisnish times
'calculate duration

Console.WriteLine("Please enter the start time: ")


startTime = Console.ReadLine

Console.WriteLine("Please enter the finish time: ")


finishTime = Console.ReadLine

runDurationSeconds = DateDiff(DateInterval.Second, startTime, finishTime)


runDurationMinutes = runDurationSeconds / 60

Console.WriteLine("duration: " & Math.Round(runDurationMinutes, 2))

'find the position of this runner id in the RunnerID array


runnerFound = False

For i = 0 To 2
If RunnerID(i) = currentRunnerID Then
runnerIDPosition = i
runnerFound = True
End If
Next

If runnerFound = False Then


Console.WriteLine("This runner id was not found in the list. Please try again")
Else
'temp instruction
Console.WriteLine("Runner id found at: " & runnerIDPosition + 1)

''update PB if needed
If PB(runnerIDPosition) = 0 Then
PB(runnerIDPosition) = runDurationMinutes
Console.WriteLine("best PB was 0. has been updated")

ElseIf runDurationMinutes < PB(runnerIDPosition) Then


Console.WriteLine("pb before update: " & PB(runnerIDPosition))
18
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur
PB(runnerIDPosition) = runDurationMinutes
Console.WriteLine("pb after update: " & PB(runnerIDPosition))
End If

'update number of runs


NumberOfRuns(runnerIDPosition) = NumberOfRuns(runnerIDPosition) + 1
If NumberOfRuns(runnerIDPosition) > 11 And NumberOfRuns(runnerIDPosition) < 22 Then
WristBand(runnerIDPosition) = "H"
ElseIf NumberOfRuns(runnerIDPosition) >= 22 Then
WristBand(runnerIDPosition) = "F"

End If
End If

End If

End While
'finding fastest Child
For i = 0 To 2
If RunnerAge(i) >= 4 And RunnerAge(i) <= 6 Then
If PB(i) < fastestTime4To6 Then
fastestTime4To6 = PB(i)
fastestPosition4To6 = (i)
End If
ElseIf RunnerAge(i) >= 7 And RunnerAge(i) <= 10 Then
fastestTime7To10 = PB(i)
fastestPosition7To10 = (i)
ElseIf RunnerAge(i) >= 11 And RunnerAge(i) <= 14 Then
fastestTime11To14 = PB(i)
fastestPosition11To14 = (i)
End If
Next

For i = 0 To 2
Console.Write("ID for runner " & i + 1 & " is: ")
Console.Write(RunnerID(i))
Console.WriteLine()

Console.Write("Name for runner " & i + 1 & " is: ")


Console.Write(RunnerName(i))
Console.WriteLine()

Console.Write("Age for runner " & i + 1 & " is: ")


Console.Write(RunnerAge(i))
Console.WriteLine()

Console.Write("PB for runner " & i + 1 & " is: ")


Console.Write(PB(i))
Console.WriteLine()

Console.Write("Num of runs for runner " & i + 1 & " is: ")
Console.Write(NumberOfRuns(i))
Console.WriteLine()

19
5 493 1972  learnatedutech@gmail.com @mmuttur
©M. Muttur
Console.Write("Name for runner " & i + 1 & " is: ")
Console.Write(RunnerName(i))
Console.WriteLine()
Console.Write("WristBand " & i + 1 & " is: ")
Console.Write(WristBand(i))
Console.WriteLine()

Console.WriteLine()
Console.WriteLine("***")
Next

Console.WriteLine("fastest runner 4 to 6: " & RunnerName(fastestPosition4To6))


Console.WriteLine("fastest time 4 to 6: " & fastestTime4To6)

Console.WriteLine("fastest runner 7 to 10: " & RunnerName(fastestPosition7To10))


Console.WriteLine("fastest time 7 to 10: " & fastestTime7To10)

Console.WriteLine("fastest runner 11 to 14: " & RunnerName(fastestPosition11To14))


Console.WriteLine("fastest time 11 to 14: " & fastestTime11To14)

Console.ReadLine()

End Sub
Function generateRunnerID() As String
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Do
Randomize()
a = Int(Rnd() * 10)
b = Int(Rnd() * 10)
c = Int(Rnd() * 10)
d = 11 - (((a * 3) + (b * 2) + (c * 1)) Mod 11)
generateRunnerID = a & b & c & d

Loop Until d < 10

End Function

End Module

Wishing you all the best for your exams!

20
5 493 1972  learnatedutech@gmail.com @mmuttur

Potrebbero piacerti anche