Question 1 Report
A 2D array stores the number of items sold by 3 shops over 4 weeks.
DECLARE Sales : ARRAY[1:3, 1:4] OF INTEGER
The array contents are:
| Week 1 | Week 2 | Week 3 | Week 4 | |
|---|---|---|---|---|
| Shop 1 | 120 | 150 | 130 | 140 |
| Shop 2 | 200 | 180 | 210 | 190 |
| Shop 3 | 90 | 110 | 100 | 95 |
The following pseudocode calculates the total sales for each shop.
FOR Shop <- 1 TO 3
DECLARE ShopTotal : INTEGER
ShopTotal <- 0
FOR Week <- 1 TO 4
ShopTotal <- ShopTotal + Sales[Shop, Week]
NEXT Week
OUTPUT "Shop ", Shop, " total: ", ShopTotal
NEXT Shop
(a) Complete the trace table showing ShopTotal at the end of each inner loop iteration for Shop 1 only. [2]
| Week | Sales[1, Week] | ShopTotal |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 |
(b) State the three lines of output produced by the program. [3]
(c) Write pseudocode to find and output the highest single weekly sales figure across all shops and weeks. [1]
(a) Trace table for Shop 1 (the inner loop with Shop = 1): [2]
| Week | Sales[1, Week] | ShopTotal |
|---|---|---|
| 1 | 120 | 120 |
| 2 | 150 | 270 |
| 3 | 130 | 400 |
| 4 | 140 | 540 |
[1] for correct Sales values from the table, [1] for correct running totals (each row adds the Sales value to the previous ShopTotal).
ShopTotal is reset to 0 before each shop's inner loop begins. For Shop 1: 0+120=120, 120+150=270, 270+130=400, 400+140=540.
(b) The three lines of output: [3]
Shop 1 total: 540
Shop 2 total: 780
Shop 3 total: 395[1] for Shop 1 = 10+20+15 is wrong - wait, for the OUTPUT, Shop 1 uses the Sales array: 120+150+130+140 = 540. [1] for Shop 2 = 200+180+210+190 = 780. [1] for Shop 3 = 90+110+100+95 = 395.
(c) Pseudocode to find the highest single weekly sales figure: [1]
DECLARE Highest : INTEGER
Highest <- Sales[1, 1]
FOR Shop <- 1 TO 3
FOR Week <- 1 TO 4
IF Sales[Shop, Week] > Highest
THEN
Highest <- Sales[Shop, Week]
ENDIF
NEXT Week
NEXT Shop
OUTPUT "Highest: ", HighestThe algorithm initialises Highest to the first element (Sales[1,1] = 120), then uses nested loops to check every element in the 2D array. Whenever a value exceeds the current Highest, it replaces it. The expected answer is 210 (Shop 2, Week 3).
Everything you need to excel in your exams