Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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
//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)
12
5 493 1972 learnatedutech@gmail.com @mmuttur
©M. Muttur
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
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
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
fastestTime4To6 = 9999
fastestTime7To10 = 9999
fastestTime11To14 = 9999
currentRunnerID = 0
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
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
runnerFound = False
'recording runs timings and updating PB if needed
While currentRunnerID <> "-1"
Console.WriteLine("Please enter the runner ID: ")
currentRunnerID = Console.ReadLine
For i = 0 To 2
If RunnerID(i) = currentRunnerID Then
runnerIDPosition = i
runnerFound = True
End If
Next
''update PB if needed
If PB(runnerIDPosition) = 0 Then
PB(runnerIDPosition) = runDurationMinutes
Console.WriteLine("best PB was 0. has been updated")
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("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.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
End Function
End Module
20
5 493 1972 learnatedutech@gmail.com @mmuttur