Choosing a 1D or 2D array for a scenario

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Choosing a 1D or 2D array for a scenario

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A teacher stores the names of 30 students in one simple list. Which array fits best?
A A 1D array, e.g. ARRAY[1:30] OF STRING
B A 2D array, e.g. ARRAY[1:30, 1:30] OF STRING
2. A noughts and crosses game board has 3 rows and 3 columns. Which array fits best?
A A 1D array, e.g. ARRAY[1:3] OF CHAR
B A 2D array, e.g. ARRAY[1:3, 1:3] OF CHAR
3. A program stores the marks of 30 students over 4 tests. Which array fits best?
A One 1D array: ARRAY[1:30] OF INTEGER
B One 2D array: ARRAY[1:30, 1:4] OF INTEGER
4. Which statement about choosing an array is correct?
A Use a 2D array when each value needs a row and a column to locate it
B Use a 2D array whenever you have more than 10 values
5. Which array would you use to find a student's mark with Marks[5, 2]?
A A 2D array, because two indexes are used
B A 1D array, because it is a list of marks

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1DECLARE Names : ARRAY[1:30] OF STRING AThe array is 1D but is used with two indexes. Marks for students and tests form a table, so it should be declared as 2D: ARRAY[1:30, 1:4] OF INTEGER.
2DECLARE Board : ARRAY[1:3, 1:3] OF CHAR BA 1D array: one simple list of 30 names, where each name is found using a single index
3Marks for 30 students over 4 tests form a table, so each mark needs a student (row) and a test (column) CNames[5] <- "Ava" Scores[5] <- 88
4Parallel 1D arrays store related lists side by side, and the same index links the matching items DDECLARE Marks : ARRAY[1:30, 1:4] OF INTEGER
5DECLARE Marks : ARRAY[1:120] OF INTEGER Marks[3, 2] <- 75 EA simple list of names needs only one index, so a 2D array is unnecessary. It should be DECLARE Names : ARRAY[1:30] OF STRING and Names[7] <- "Zoe".
6DECLARE Names : ARRAY[1:30, 1:1] OF STRING Names[7, 1] <- "Zoe" FA 2D array: a 3 by 3 game board grid, where each square is found using a row and a column

Definitions

1D or 2D array

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
DECLARE Names : ARRAY[____] OF STRING
2.
DECLARE Board : ARRAY[1:3, ___] OF CHAR
3.
DECLARE Marks : ARRAY[1:30, ___] OF INTEGER
4.
// mark for each student and test OUTPUT _____[Student, Test]
5.
// parallel 1D arrays share one index OUTPUT Names[I], Marks[_]

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Names[5,1] ← "Zoe"
B Names[6] ← "Leo"
C OUTPUT Names[5]
Question 2
A Board[2] ← "X"
B Board[1,3] ← "O"
C OUTPUT Board[2,2]
Question 3
A Marks[1,1] ← 72
B Marks[1,2] ← 65
C Marks[2,1] ← 58
D Marks[2] ← 80
Question 4
A Name[3] ← "Mia"
B Mark[4] ← 91
C OUTPUT Name[3], Mark[3]
Question 5
A FOR r ← 1 TO 3
B FOR r ← 1 TO 3
C Board[r,c] ← "-"
D NEXT c
E NEXT r

Answer Key

Choosing a 1D or 2D array for a scenario
Section A: Quick Questions
Q1
A — A 1D array, e.g. ARRAY[1:30] OF STRING
Q2
B — A 2D array, e.g. ARRAY[1:3, 1:3] OF CHAR
Q3
B — One 2D array: ARRAY[1:30, 1:4] OF INTEGER
Q4
A — Use a 2D array when each value needs a row and a column to locate it
Q5
A — A 2D array, because two indexes are used
Section B: Match the Pairs
Q1
DECLARE Names : ARRAY[1:30] OF STRING → A 1D array: one simple list of 30 names, where each name is found using a single index
Q2
DECLARE Board : ARRAY[1:3, 1:3] OF CHAR → A 2D array: a 3 by 3 game board grid, where each square is found using a row and a column
Q3
Marks for 30 students over 4 tests form a table, so each mark needs a student (row) and a test (column) → DECLARE Marks : ARRAY[1:30, 1:4] OF INTEGER
Q4
Parallel 1D arrays store related lists side by side, and the same index links the matching items → Names[5] <- "Ava" Scores[5] <- 88
Q5
DECLARE Marks : ARRAY[1:120] OF INTEGER Marks[3, 2] <- 75 → The array is 1D but is used with two indexes. Marks for students and tests form a table, so it should be declared as 2D: ARRAY[1:30, 1:4] OF INTEGER.
Q6
DECLARE Names : ARRAY[1:30, 1:1] OF STRING Names[7, 1] <- "Zoe" → A simple list of names needs only one index, so a 2D array is unnecessary. It should be DECLARE Names : ARRAY[1:30] OF STRING and Names[7] <- "Zoe".
Section C: Definitions
Q1
1D or 2D array — Use a 1D array for a simple list of values. Use a 2D array when data forms a table or grid, so each value needs two indexes: a row and a column.
Section D: Fill in the Blanks
Q1
DECLARE Names : ARRAY[1:30] OF STRING
Q2
DECLARE Board : ARRAY[1:3, 1:3] OF CHAR
Q3
DECLARE Marks : ARRAY[1:30, 1:4] OF INTEGER
Q4
// mark for each student and test OUTPUT Marks[Student, Test]
Q5
// parallel 1D arrays share one index OUTPUT Names[I], Marks[I]
Section E: Spot the Error
Q1
A — A list of 30 names is a 1D array, so each name needs only one index. It should be Names[5] ← "Zoe".
Q2
A — A game board is a grid, so it is a 2D array and each square needs a row and a column. It should be Board[2,2] ← "X".
Q3
D — Marks for students across several tests form a table, so a 2D array. You need the student (row) and the test (column): Marks[2,2] ← 80.
Q4
B — With parallel 1D arrays, the same index must be used for the same student. Mia is at index 3 in Name, so her mark must be Mark[3] ← 91.
Q5
B — A 2D board needs two different loop variables, one for rows and one for columns. The inner loop should be FOR c ← 1 TO 3, because Board[r,c] uses c.