Programming and Software Development: Question 7
Syllabus 11.1, 11.2
A gym records how many times each of 5 members visited during the last month, storing these
counts in the array Visits. The pseudocode below classifies each member using their number of
visits, and counts how many members qualify as "Regular" (9 or more visits in the month).
DECLARE Visits : ARRAY[1:5] OF INTEGER
DECLARE i : INTEGER
DECLARE Category : STRING
DECLARE RegularCount : INTEGER
Visits[1] ← 0
Visits[2] ← 5
Visits[3] ← 12
Visits[4] ← 8
Visits[5] ← 20
RegularCount ← 0
FOR i ← 1 TO 5
IF Visits[i] = 0
THEN
Category ← "Inactive"
ELSE
IF Visits[i] >= 9
THEN
Category ← "Regular"
RegularCount ← RegularCount + 1
ELSE
Category ← "Occasional"
ENDIF
ENDIF
OUTPUT Visits[i], " : ", Category
NEXT i
OUTPUT "Regular members: ", RegularCount
(a) Copy and complete a trace table showing, for each of the 5 passes through the FOR loop,
the value of i, the value of Visits[i], the Category assigned on that pass, and the value
of RegularCount after that pass. [4]
(b) State the value output by the final line, OUTPUT "Regular members: ", RegularCount. [1]
(c) State why a FOR ... NEXT loop (a count-controlled loop) is a sensible choice for this
task, referring to the length of the Visits array. [1]
(d) The gym wants to adapt this code to work for a list of members whose length is not known in
advance, and could be different each month. State whether a FOR ... NEXT loop is still the
most suitable choice for stepping through this new list, name a more suitable loop construct if
not, and give one reason for your choice. [2]
Show worked solution Hide worked solution
Worked solution
Part (a): Tracing the FOR loop and nested IF
RegularCount starts at 0. On each pass, the outer IF checks whether Visits[i] = 0; if not,
the inner IF checks whether Visits[i] >= 9.
| Pass | i | Visits[i] | Category | RegularCount after this pass |
|---|---|---|---|---|
| 1 | 1 | 0 | Inactive | 0 |
| 2 | 2 | 5 | Occasional | 0 |
| 3 | 3 | 12 | Regular | 1 |
| 4 | 4 | 8 | Occasional | 1 |
| 5 | 5 | 20 | Regular | 2 |
- Pass 1:
Visits[1] = 0, so the outerIFis true andCategory ← "Inactive"; the innerIFis never reached, soRegularCountstays at 0. - Pass 2:
Visits[2] = 5, not 0, so the innerIFis checked:5 >= 9is false, soCategory ← "Occasional";RegularCountstays at 0. - Pass 3:
Visits[3] = 12, not 0;12 >= 9is true, soCategory ← "Regular"andRegularCountbecomes 1. - Pass 4:
Visits[4] = 8, not 0;8 >= 9is false, soCategory ← "Occasional";RegularCountstays at 1. - Pass 5:
Visits[5] = 20, not 0;20 >= 9is true, soCategory ← "Regular"andRegularCountbecomes 2.
[4 marks]: [1] for correct values on passes 1–2, [1] for correct values on pass 3
(including RegularCount becoming 1), [1] for correct values on pass 4, [1] for correct
values on pass 5 (including RegularCount becoming 2).
Part (b): The final output
After the loop finishes, RegularCount = 2 (from members 3 and 5, with 12 and 20 visits). The
final line therefore outputs:
Regular members: 2
[1 mark] for the correct value, 2.
Part (c): Why FOR…NEXT suits this task
Visits is declared as ARRAY[1:5] OF INTEGER, a fixed size of exactly 5 elements, and every
element must be processed exactly once. Because the number of repetitions needed (5) is known
before the loop even starts, a FOR ... NEXT loop, a count-controlled loop, is a sensible
choice: it repeats a known, fixed number of times without needing any condition to be tested
against changing data just to decide whether to continue. [1 mark].
Part (d): Adapting the code for an unknown number of members
If the number of members is not fixed and could change each month, a FOR ... NEXT loop is no
longer the most suitable choice, since it requires the exact number of repetitions to be written
into the loop header before it starts running. A WHILE ... DO loop would be more suitable:
it tests, before each pass, whether there is still another member’s data left to process (for
example, using a sentinel value or an “end of list” flag), so it naturally repeats exactly as
many times as there are members, whatever that number turns out to be that month. [2 marks]:
[1] for stating FOR...NEXT is no longer the most suitable and naming WHILE...DO (or an
equivalent pre-condition, data-driven loop) instead, [1] for a valid reason referring to the
list length no longer being known in advance.
Final answers
- (a)
RegularCountreaches 0, 0, 1, 1, 2 after passes 1–5, withCategoryvalues Inactive, Occasional, Regular, Occasional, Regular in that order. - (b)
Regular members: 2. - (c)
FOR...NEXTis suitable because the number of repetitions (5) is fixed and known in advance, matching the size of theVisitsarray. - (d)
FOR...NEXTis no longer the most suitable choice; aWHILE...DOloop is more suitable, since it can repeat until the (now unknown) number of members has been processed.