Teachers' Portal  /  A-Level Computer Science 9618  /  Data types and structures  /  1D and 2D arrays
Lesson pack · A-Level Computer Science 9618 · 10.2 · AS Level

1D and 2D arrays

Many values, one name. An array stores a fixed number of values of the same data type under one identifier, each reached by its index.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Data types and structures
Syllabus reference
Cambridge International AS and A Level Computer Science 9618, 10.2
Level
AS Level
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 2 of 5 in Data types and structures; follows Data types and records; leads into Linear search and bubble sort
Central model
Fig. 1: a 1D array of six integers and a 2D array of 3 rows by 4 columns
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • One dimension: Declare the array with its bounds and data type, then use a loop variable as the index to visit every element in turn.
  • Two dimensions: In the lab, run the 2D array program: it fills a 2 by 3 table with nested loops and totals each row.
  • Choosing a structure: Examiners ask you to select a 1D or 2D array and justify it.
Key points to leave on the board
  • Elements = upper bound − lower bound + 1; a 2D array has rows × columns.
  • Declare: DECLARE X : ARRAY[1:n] OF type or ARRAY[1:r, 1:c] OF type.
  • Process: a FOR loop for 1D; nested FOR loops for 2D, with totals reset in the right loop.
  • Choose: 2D when each value has a row and a column; records when the data types differ.
The core visual model

One picture every idea returns to

An array is a fixed-length data structure of elements that all have the same data type, held under one identifier. Each element is reached by its index (subscript). The lower bound is the index of the first element and the upper bound the index of the last, so ARRAY[1:30] has 30 elements. A 1D array is a list with one index; a 2D array is a table reached by [row, column]. Arrays are processed one element at a time with a FOR loop; a 2D array needs nested loops.

Scores : ARRAY[1:6] OF INTEGER 1231731259 123456 lower bound upper bound Marks : ARRAY[1:3, 1:4] OF INTEGER 5869 77104 3986 row 1row 2row 3 Marks[2, 3] = 10
Fig. 1: a 1D array of six integers and a 2D array of 3 rows by 4 columns
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, Many values, one name.. An array stores a fixed number of values of the same data type under one identifier, each reached by its index. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: One dimensionDeclare the array with its bounds and data type, then use a loop variable as the index to visit every element in turn. Model the worked example Trace table on the board, one line at a time. Your turn: Write a FOR loop that sets every element of Names: ARRAY[1:30] OF STRING to the empty string.Whole class, then pairs
13 to 21 minTeach and check: Two dimensionsIn the lab, run the 2D array program: it fills a 2 by 3 table with nested loops and totals each row. Project tab 6 of the lab (2D arrays with nested loops) and let a student drive it while the class predicts. Model the worked example Row totals on the board, one line at a time. Your turn: Swap the loops so that the outer loop is FOR Column ← 1 TO 4. What total is output for column 3?Pairs
21 to 30 minTeach and check: Choosing a structureExaminers ask you to select a 1D or 2D array and justify it. Model the worked example Select and declare on the board, one line at a time.Corners
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 23 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in Choosing a structure, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Corners

Put one answer or viewpoint in each corner of the room. Students move to the corner they agree with, justify the choice to a partner there, then hear one voice from each corner before the reveal.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
Start the maximum at the first element, Max ← Scores[1], not at 0: if every value were negative, a start of 0 would give the wrong answer.Return to One dimension: ask a pair to state the correct version and show it on the lab before moving on.
Keep the index order the same as the declaration.If the array is [1:3, 1:4], then Marks[4, 2] is out of range: there is no row 4.
Do not mix data types in one array. "A 2D array with a column of names and a column of marks" would need every element to be STRING, so the marks could not be used in arithmetic without conversion.Return to Choosing a structure: ask a pair to state the correct version and show it on the lab before moving on.
Worksheet

1D and 2D arrays: worksheet

6 questions, 23 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · 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.

Q2 · write[3 marks]

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

Q3 · 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
Q4 · 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.

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

Q6 · 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
Answer key and marking notes
Q1 · 3 marks
  • (a) 0.
  • (b) 23.
  • (c) 23 − 0 + 1 = 24.
Q2 · 3 marks
  • DECLARE Names : ARRAY[1:50] OF STRING.
  • FOR Index ← 1 TO 50 ... NEXT Index.
  • Names[Index] ← "" inside the loop.
Q3 · 4 marks
  • 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.
Q4 · 6 marks
  • 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.
Q5 · 4 marks
  • (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.
Q6 · 3 marks
  • 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.
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

How many elements does DECLARE Seats : ARRAY[1:6, 0:9] OF BOOLEAN create?

  • A60
  • B54
  • C50
  • D45
Q21 mark

Which statement about an array in 9618 pseudocode is correct?

  • AEvery element has the same data type, and the number of elements is fixed when it is declared
  • BEach element can have a different data type, chosen when a value is stored
  • CThe upper bound is the largest value currently stored in the array
  • DThe array grows by one element each time a new value is assigned
Q31 mark

Grid : ARRAY[1:3, 1:4] OF INTEGER holds the values below, indexed [row, column]. What is the value of Grid[2, 3]?

Column 1Column 2Column 3Column 4
Row 16294
Row 23857
Row 31462

  • A5
  • B4
  • C8
  • D6
Q41 mark

Nums holds 4, 7, 1, 9, 3, 6 in elements 1 to 6. What is output?

Total ← 0
FOR i ← 1 TO 6
    IF Nums[i] MOD 2 = 1 THEN
        Total ← Total + Nums[i]
    ENDIF
NEXT i
OUTPUT Total

  • A20
  • B10
  • C30
  • D4
Q51 mark

Grid holds the values below. What is output by the pseudocode?

Column 1Column 2Column 3Column 4
Row 16294
Row 23857
Row 31462
Best ← -1
BestCol ← 0
FOR Col ← 1 TO 4
    ColTotal ← 0
    FOR Row ← 1 TO 3
        ColTotal ← ColTotal + Grid[Row, Col]
    NEXT Row
    IF ColTotal > Best THEN
        Best ← ColTotal
        BestCol ← Col
    ENDIF
NEXT Col
OUTPUT BestCol, " ", Best

  • A3 20
  • B2 23
  • C3 9
  • D4 13
Q61 mark

Data is declared as ARRAY[1:10] OF INTEGER. This code is meant to shift every value one place towards the start. What goes wrong?

FOR i ← 1 TO 10
    Data[i] ← Data[i + 1]
NEXT i

  • AOn the last pass it refers to Data[11], which is outside the bounds of the array
  • BOn the first pass it refers to Data[0], which is outside the bounds of the array
  • CEvery element ends up holding the value that was in Data[1]
  • DThe values are shifted one place towards the end instead of the start
Quiz key
Q1: A

The first dimension has 6 − 1 + 1 = 6 indices and the second 9 − 0 + 1 = 10, so 6 × 10 = 60 elements. 54 uses 9 for the second dimension (upper minus lower without the + 1), 50 uses 5 for the first, and 45 makes the same slip in both.

Q2: A

An array is a fixed-length structure of elements that all share the declared data type. Mixed data types need a record. The bounds are indices (the first and last positions), not stored values, and an array does not grow: its size is set by its declaration.

Q3: A

Row 2 is 3, 8, 5, 7, and its third column holds 5. 4 is Grid[3, 2] (row and column swapped), 8 is Grid[2, 2], and 6 is Grid[3, 3].

Q4: A

The odd values are 7, 1, 9 and 3, and 7 + 1 + 9 + 3 = 20. 10 adds the even values (4 + 6), 30 adds every element, and 4 is how many odd values there are rather than their total.

Q5: A

The column totals are 10, 14, 20 and 13, so the largest is column 3 with 20. 2 23 is the largest row total (3 + 8 + 5 + 7), which comes from mixing up rows and columns. 3 9 picks the right column but reports its largest single element, and 4 13 is simply the last column processed.

Q6: A

When i = 10 the statement reads Data[11], but the upper bound is 10, so this is an out-of-range error; the loop should run to 9. Data[0] is never used, since i starts at 1. Each element correctly receives the next value along (no value is copied into every element), and copying from i + 1 to i moves values towards the start.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonData types and records