Procedures, Functions, Arrays and File Handling: Question 3
Syllabus 8.1, 8.2
A fitness app stores the number of steps a user walked on each of 6 days of the week in a one-dimensional array, StepCounts, indexed from 1 to 6, where index 1 represents Monday and index 6 represents Saturday.
DECLARE StepCounts : ARRAY[1:6] OF INTEGER
DECLARE Day : INTEGER
DECLARE Total : INTEGER
StepCounts[1] ← 4200
StepCounts[2] ← 6100
StepCounts[3] ← 3950
StepCounts[4] ← 7300
StepCounts[5] ← 5800
StepCounts[6] ← 6650
Total ← 0
FOR Day ← 1 TO 6
Total ← Total + StepCounts[Day]
NEXT Day
(a) State the value of StepCounts[4]. [1]
(b) Trace the FOR loop and state the value of Total once it has finished running. [2]
(c) A function, MostActiveDay, is used to find the index of the day with the highest number of steps. Complete the pseudocode below by writing the pseudocode statement missing at each of the two lines labelled (i) and (ii).
FUNCTION MostActiveDay(Steps : ARRAY[1:6] OF INTEGER) RETURNS INTEGER
DECLARE Best : INTEGER
DECLARE Day : INTEGER
Best ← 1
FOR Day ← 2 TO 6
IF Steps[Day] > Steps[Best]
THEN
_____(i)_____
ENDIF
NEXT Day
_____(ii)_____
ENDFUNCTION
[2]
(d) State the value that the call MostActiveDay(StepCounts) would return for the data given above, and name the day of the week (Monday to Saturday) that this index represents. [2]
Show worked solution Hide worked solution
Worked solution
Part (a): Reading a single array element
Arrays in this pseudocode are indexed from 1, so StepCounts[4] is exactly the fourth value
assigned above: StepCounts[4] ← 7300. So StepCounts[4] = 7300.
Part (b): Tracing the FOR loop that totals all 6 days
Total starts at 0, and the loop adds StepCounts[Day] for Day from 1 to 6:
| Day | StepCounts[Day] | Total after this step |
|---|---|---|
| 1 | 4200 | 0 + 4200 = 4200 |
| 2 | 6100 | 4200 + 6100 = 10300 |
| 3 | 3950 | 10300 + 3950 = 14250 |
| 4 | 7300 | 14250 + 7300 = 21550 |
| 5 | 5800 | 21550 + 5800 = 27350 |
| 6 | 6650 | 27350 + 6650 = 34000 |
Once the loop finishes (after Day = 6), Total = 34000.
Part (c): Completing the function that finds the most active day
MostActiveDay keeps track of the best index seen so far in Best, starting at 1 (Day 1 is
assumed best until proven otherwise). Each time a later day’s step count is strictly greater than
the step count at the current best index, Best must be updated to that day’s index, not the
step count itself:
(i) Best ← Day
Once every day from 2 to 6 has been checked, the function must send the final index back to
whichever line called it, using RETURN:
(ii) RETURN Best
Part (d): Tracing MostActiveDay(StepCounts)
Best ← 1 (Steps[1] = 4200). Then, for Day from 2 to 6:
| Day | Steps[Day] | Steps[Best] | Steps[Day] > Steps[Best]? | Best after this step |
|---|---|---|---|---|
| 2 | 6100 | 4200 | Yes | 2 |
| 3 | 3950 | 6100 | No | 2 |
| 4 | 7300 | 6100 | Yes | 4 |
| 5 | 5800 | 7300 | No | 4 |
| 6 | 6650 | 7300 | No | 4 |
The function returns Best = 4. Since Day 1 = Monday, Day 4 corresponds to Thursday, and
this matches part (a), where StepCounts[4] = 7300 was already identified as the largest value
in the array.
Final answers
- (a) StepCounts[4] = 7300
- (b) Total = 34000
- (c) (i)
Best ← Day(ii)RETURN Best - (d) MostActiveDay(StepCounts) returns 4, which is Thursday