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 ...

Assessment: Computer Science (9-1) 0984 | Paper 2 Mock 01 | Algorithms, Programming and Logic Subject: Computer Science (9-1) - 0984

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 1Week 2Week 3Week 4
Shop 1120150130140
Shop 2200180210190
Shop 39011010095

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]

WeekSales[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]

Answer Details

(a) Trace table for Shop 1 (the inner loop with Shop = 1): [2]

WeekSales[1, Week]ShopTotal
1120120
2150270
3130400
4140540

[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: ", Highest

The 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).

Download The App On Google Playstore

Everything you need to excel in your exams

Green Bridge CBT Mobile App
Personalized AI Learning Chat Assistant
200,000+ Exam Questions Across IGCSE, JAMB, WAEC & NECO
Over 3,900 Lesson Notes
Offline Support - Learn Anytime, Anywhere
Green Bridge Timetable
Literature Summaries & Potential Questions
Track Your Performance & Progress
In-depth Explanations for Comprehensive Learning