Date | May 2018 | Marks available | 4 | Reference code | 18M.1.SL.TZ0.12 |
Level | SL | Paper | 1 | Time zone | no time zone |
Command term | Construct | Question number | 12 | Adapted from | N/A |
Question
A transport authority is investigating how many people use a certain direct train route, which is used every day of the week.
At the end of each day, the total number of passengers who travelled on this route is stored in a collection, PASSENGERS
.
The first item was written to the collection on Monday 1st January 2018.
The next items, collected on Tuesday and Wednesday, were added like this:
The collection DATA
contains the following data:
2, 4, 1, −2, −4, 1, 0
Consider the following pseudocode:
COUNTER = 0
SUM = 0
DATA.resetNext()
loop for X from 0 to 6
if DATA.getNext() > 0
ARRAY[X] = DATA.getNext()
COUNTER = COUNTER + 1
SUM = SUM + ARRAY[X]
end if
end loop
output SUM/COUNTER
Trace the pseudocode using the table below:
Assuming that the first item read from the collection is from Monday 1st January 2018, construct pseudocode that will read PASSENGERS
into an array, P_ARRAY
.
Using P_ARRAY
, construct pseudocode to output the day of the week with the highest average number of passengers. Use the sub procedure convert()
which converts the numbers 0 to 6 into days of the week, for example convert(1)
will return “Tuesday”.
Note: you should not assume that data for an exact number of weeks is stored.
Markscheme
Award [1] for each correct column (excepting the first). If the first row is not completed, mark as follows:
All 4 correct except for first row: [3]
3 correct except for first row: [2]
2 correct except for first row: [1]
1 correct except for first row: [0].
Follow through for an output that is correct from their incorrect COUNTER
or SUM
columns.
P_ARRAY // array declaration
X = 0
(PASSENGERS.resetNext())
while PASSENGERS.hasNext()
P_ARRAY[X] = PASSENGERS.getNext()
X = X + 1
end while
Award [1] for evidence of the idea of a loop (for or while etc.).
Award [1] for correct while/until loop with correct condition.
Award [1] for correct reading of collection into array.
Award [1] for declaring and incrementing X.
The following are two alternative approaches. Most solutions should fit one or the other markscheme. Do not penalise those who introduce separate variables instead of arrays.
Example 1:
MAXAV = 0
X = (P_ARRAY.length) − 1
MAXDAY
loop for DAY from 0 to 6
SUM = 0
COUNTER = 0
loop for C from DAY to X //loop while C < X
SUM = SUM + P_ARRAY[C]
COUNTER = COUNTER + 1
C = C + 6 // C = C + 7 or allow step 7 in the loop
end loop
if SUM/COUNTER > MAXAV
MAXAV = SUM/COUNTER
MAXDAY = DAY
end if
end loop
output convert(MAXDAY)
Award [1] for correct initialization of numerical variables (e.g. MAXAV
, COUNTER
, SUM
).
Award [1] for correct loop through each day.
Award [1] for correct for loop through array.
Award [1] for incrementing array position by 7 each time.
Award [1] for calculation of average (SUM/COUNTER
).
Award [1] for comparing this with MAXAV
.
Award [1] for adjusting MAXAV
and MAXDAY
if necessary.
Award [1] for correct use of CONVERT
sub-procedure.
Example 2:
MAXDAY = 0
DAYSUM ARRAY
WEEKS ARRAY
COUNT = 0
while COUNT < P_ARRAY.length //loop through p.array
DAY = COUNT MOD 7
WEEKS[DAY] = WEEKS[DAY] + 1 //add number of weeks for this day
DAYSUM[DAY] = DAYSUM[day] + P_ARRAY[COUNT]
COUNT = COUNT + 1
end while
COUNT = 0 //reinitialize
while COUNT < DAYSUM.length //loop through dayArray can also be
//a for loop as there are 7 days.
if DAYSUM[COUNT] / WEEKS[COUNT] > MAXDAY //find maxDay
MAXDAY = COUNT
end if
COUNT = COUNT + 1
end while
output convert(MAXDAY)
Award [1] for correct initialisation of variables/arrays.
Award [1] for correct loop through array.
Award [1] mark for incrementing day.
Award [1] mark for incrementing sum for each day.
Award [1] mark for incrementing number of weeks for each day.
Award [1] mark for correct calculation of average for each day.
Award [1] mark for finding MAXDAY.
Award [1] mark for correct use of CONVERT procedure.