Algorithm Design and Standard Methods: Question 5
Syllabus 7.4
A fitness app records the number of steps a user walks on each of the 30 days of a month, stored in a one-dimensional (1D) array called Steps. The pseudocode algorithm below processes this data.
DECLARE Steps : ARRAY[1:30] OF INTEGER
DECLARE Day : INTEGER
DECLARE Total : INTEGER
Total ← 0
FOR Day ← 1 TO 30
Total ← Total + Steps[Day]
NEXT Day
OUTPUT Total / 30
What does this algorithm calculate?
Show worked solution Hide worked solution
Worked solution
Step 1: Trace the loop
Total starts at 0. For every day from 1 to 30, Total ← Total + Steps[Day] adds that day’s
step count onto the running total, with no condition attached. Every one of the 30 values gets
added, regardless of how large or small it is. After the loop, Total holds the sum of all 30
days’ steps.
Step 2: Look at what happens after the loop
The final line is OUTPUT Total / 30. This does not output Total on its own, it divides the
sum by 30, the fixed number of days in the month.
Step 3: Rule out the other standard methods
- Totalling alone (option A) describes only what
Totalholds before the final line; the division that follows means the algorithm does not stop at a total. - Counting (option B) would require an
IFstatement testing a condition (such asSteps[Day] > 30) and incrementing a counter only when it is true. No such condition appears anywhere in this algorithm. - Finding a maximum (option D) would require comparing each day’s steps against the largest value seen so far and keeping that value, not adding every value into a running total.
Step 4: Confirm what is being calculated
Summing every value and then dividing by the fixed number of values is exactly the standard “totalling and dividing by the count” method used to find an average (mean).
Final answer
- The algorithm calculates the average number of steps walked per day during the month, option C.