Procedures, Functions, Arrays and File Handling: Question 3

Syllabus 8.1, 8.2

Structured 7 marks

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:

DayStepCounts[Day]Total after this step
142000 + 4200 = 4200
261004200 + 6100 = 10300
3395010300 + 3950 = 14250
4730014250 + 7300 = 21550
5580021550 + 5800 = 27350
6665027350 + 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:

DaySteps[Day]Steps[Best]Steps[Day] > Steps[Best]?Best after this step
261004200Yes2
339506100No2
473006100Yes4
558007300No4
666507300No4

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