AS · Practice questions · 1D and 2D arrays

Index into the table.

Six original Cambridge-style questions on 1D and 2D arrays, with a trace table and pseudocode mark points.

Original questions All questions on this page are original work, written in the Cambridge AS & A Level style. They are not from past papers. They test the same concepts and skills the syllabus rewards.
Keep these straight

Bounds, then loops.

01
Recall
[3 marks]

An array is declared as DECLARE Temps : ARRAY[0:23] OF REAL. State (a) the lower bound, (b) the upper bound, (c) the number of elements.

  • (a) 0. ✓
  • (b) 23. ✓
  • (c) 23 − 0 + 1 = 24. ✓
02
Write
[3 marks]

Write pseudocode to declare a 1D array Names to hold 50 names, and to set every element to the empty string.

  • DECLARE Names : ARRAY[1:50] OF STRING. ✓
  • FOR Index ← 1 TO 50 ... NEXT Index. ✓
  • Names[Index] ← "" inside the loop. ✓
03
Trace
[4 marks]

Nums holds 3, 8, 2, 8, 5, 1 in elements 1 to 6. Complete a trace table for this pseudocode, state the output and describe what it does.

Count ← 0
Last ← Nums[1]
FOR i ← 2 TO 6
    IF Nums[i] > Last THEN
        Count ← Count + 1
    ENDIF
    Last ← Nums[i]
NEXT i
OUTPUT Count
  • i = 2: 8 > 3, Count = 1, Last = 8; i = 3: 2 > 8 FALSE, Last = 2. ✓
  • i = 4: 8 > 2, Count = 2, Last = 8; i = 5: 5 > 8 FALSE, Last = 5; i = 6: 1 > 5 FALSE, Last = 1. ✓
  • Output: 2. ✓
  • It counts how many values are greater than the value immediately before them. ✓
04
Write
[6 marks]

Sales : ARRAY[1:4, 1:12] OF REAL holds the sales of 4 shops (rows) for 12 months (columns). Write pseudocode to output the total sales for each month, and then the number of the month with the highest total.

  • Initialise BestTotal ← -1 (or the total of month 1) and BestMonth ← 1. ✓
  • Outer loop FOR Month ← 1 TO 12; set MonthTotal ← 0 inside it. ✓
  • Inner loop FOR Shop ← 1 TO 4 with MonthTotal ← MonthTotal + Sales[Shop, Month]. ✓
  • After the inner loop: OUTPUT Month, MonthTotal. ✓
  • IF MonthTotal > BestTotal THEN BestTotal ← MonthTotal, BestMonth ← Month. ✓
  • After the outer loop: OUTPUT BestMonth. ✓
05
Apply
[4 marks]

A theatre has 12 rows of 25 seats. The program records whether each seat is booked. (a) Choose a suitable data structure, justify your choice and write its declaration. (b) Write a statement to record that seat 17 in row 5 is booked.

  • (a) A 2D array, because each seat is identified by a row and a seat number. ✓
  • Of BOOLEAN, because each seat is either booked or not. ✓
  • DECLARE Seats : ARRAY[1:12, 1:25] OF BOOLEAN. ✓
  • (b) Seats[5, 17] ← TRUE. ✓
06
Analysis
[3 marks]

This pseudocode should output the total of all 48 values in Sales, but the output is wrong. Identify the error, explain its effect and write the correction.

FOR Shop ← 1 TO 4
    Total ← 0
    FOR Month ← 1 TO 12
        Total ← Total + Sales[Shop, Month]
    NEXT Month
NEXT Shop
OUTPUT Total
  • Total ← 0 is inside the outer loop. ✓
  • Total is reset for every shop, so only the total of shop 4 is output. ✓
  • Move Total ← 0 to before FOR Shop ← 1 TO 4. ✓

Mark this once you have attempted all six and checked your working. It records a Practiced badge on the topic and adds a one-time bonus. Revealing the solutions alone does not count.