Algorithm Design and Standard Methods: Question 1
Syllabus 7.4
A gym stores the number of visits made by each of its members during the last month in a one-dimensional (1D) array called Visits, which has 8 elements. The pseudocode algorithm below is run on this array.
DECLARE Visits : ARRAY[1:8] OF INTEGER
DECLARE Index : INTEGER
DECLARE Reward : INTEGER
Reward ← 0
FOR Index ← 1 TO 8
IF Visits[Index] > 10 THEN
Reward ← Reward + 1
ENDIF
NEXT Index
OUTPUT Reward
What is the purpose of this algorithm?
Show worked solution Hide worked solution
Worked solution
Step 1: Identify the variables and what each one holds
Visits holds one value per member (8 members in total). Reward starts at 0 before the loop
runs, so it is being built up rather than storing a single fixed value read from the array.
Step 2: Trace what happens inside the loop
For every member in turn (Index from 1 to 8):
- The algorithm checks
IF Visits[Index] > 10 THEN. - Only when that condition is true does it execute
Reward ← Reward + 1.
Crucially, the amount added to Reward is always exactly 1, never Visits[Index] itself.
That single detail rules out totalling (option A), which would need Reward ← Reward + Visits[Index].
Step 3: Rule out the other standard methods
- Totalling would add each member’s actual visit count to a running total, not the case here.
- Finding a maximum would need a comparison against a variable holding the largest value seen
so far (e.g.
IF Visits[Index] > MaxSoFar THEN MaxSoFar ← Visits[Index]), and would output that variable, not a running total of “how many times”. - Finding an average would need a running total divided by the number of elements (8) at the end.
Step 4: Confirm what the algorithm actually does
Reward only ever increases by 1, and only when a member’s visits exceed 10. So after the loop,
Reward holds a count of how many of the 8 members visited more than 10 times, exactly the
standard “counting” method applied with a condition.
Final answer
- The algorithm counts how many members made more than 10 visits, option B.