Programming Constructs and Operators: Question 3
Syllabus 8.1
A weekend fundraiser records the donation made by each of 6 donors, together with whether the donation was made in cash. This pseudocode algorithm totals all of the donations, and also counts how many donations were both at least $20 and made in cash.
01 DECLARE Donation : REAL
02 DECLARE IsCash : BOOLEAN
03 DECLARE Total : REAL
04 DECLARE BigCashCount : INTEGER
05 DECLARE Counter : INTEGER
06 Total ← 0
07 BigCashCount ← 0
08 FOR Counter ← 1 TO 6
09 INPUT Donation
10 INPUT IsCash
11 Total ← Total + Donation
12 IF Donation >= 20 AND IsCash = TRUE THEN
13 BigCashCount ← BigCashCount + 1
14 ENDIF
15 NEXT Counter
16 OUTPUT Total
17 OUTPUT BigCashCount
The algorithm is run once, using this donation data, entered in the order shown:
| Donor | Donation ($) | IsCash |
|---|---|---|
| 1 | 15 | TRUE |
| 2 | 25 | TRUE |
| 3 | 30 | FALSE |
| 4 | 20 | TRUE |
| 5 | 50 | FALSE |
| 6 | 22 | TRUE |
(a) State the line number of the statement that performs totalling, and the line number of the statement that performs counting. Explain how you identified each one. [2]
(b) Complete a full trace table showing the value of Total and BigCashCount after each pass
of the loop, for the data given above. [4]
(c) State the values output for Total and BigCashCount when the algorithm finishes. [2]
Show worked solution Hide worked solution
Worked solution
Part (a): Identifying totalling and counting
- Totalling. Line 11:
Total ← Total + Donationadds the actual value ofDonationto the running total on every single pass of the loop (it is not inside theIF), soTotalends up holding the sum of all 6 donations. - Counting. Line 13:
BigCashCount ← BigCashCount + 1always adds exactly 1, never the value ofDonationitself, and it only runs when theIFcondition on line 12 is true. This is the signature of a counting statement: it counts how many times something happens, not how much. [2 marks]: [1] for correctly identifying each line, [1] for a correct explanation of the totalling/counting distinction.
Part (b): Tracing the FOR loop
The loop runs once for Counter = 1 through Counter = 6 inclusive. Total starts at 0 and
BigCashCount starts at 0 (lines 06–07). On each pass, Total always increases by Donation.
BigCashCount only increases when Donation >= 20 AND IsCash = TRUE.
| Counter | Donation | IsCash | Donation >= 20 AND IsCash = TRUE? | Total | BigCashCount |
|---|---|---|---|---|---|
| 1 | 15 | TRUE | 15 >= 20 is false → false | 15 | 0 |
| 2 | 25 | TRUE | 25 >= 20 and TRUE → true | 40 | 1 |
| 3 | 30 | FALSE | 30 >= 20 but FALSE → false | 70 | 1 |
| 4 | 20 | TRUE | 20 >= 20 and TRUE → true | 90 | 2 |
| 5 | 50 | FALSE | 50 >= 20 but FALSE → false | 140 | 2 |
| 6 | 22 | TRUE | 22 >= 20 and TRUE → true | 162 | 3 |
[4 marks]: [1] for a correct running Total column, [1] for correctly totalling all 6
donations to 162, [1] for correctly applying the AND condition on each row (in particular,
recognising that Donor 4’s exact $20 donation still satisfies >= 20), [1] for a correct
final BigCashCount of 3.
Note that Donor 1 (cash, but only 22, cash) are the kind of pair that
distinguishes AND from OR: under AND, both parts of the condition must be true, so Donor
1 fails on the amount even though the payment method matches.
Part (c): Final output
After Counter reaches 6 and the loop ends, line 16 outputs Total and line 17 outputs
BigCashCount:
Total = 162BigCashCount = 3
[2 marks]: [1] for each correctly stated final value.
Final answers
- (a) Line 11 totals the donations; line 13 counts them (adds 1 each time the condition is met).
- (b) Trace table as shown above. Total rises 15, 40, 70, 90, 140, 162; BigCashCount rises 0, 1, 1, 2, 2, 3.
- (c) Total = 162, BigCashCount = 3.