Reading a value from a 2D array element

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

Reading a value from a 2D array element

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement displays the value at row 2, column 3 of NoughtsAndCrosses?
A OUTPUT NoughtsAndCrosses[2,3]
B OUTPUT NoughtsAndCrosses[3,2]
2. What does Marks[1,2] give you in an expression?
A The value at row 1, column 2
B The value at column 1, row 2
3. Which line reads an element of a 2D array into a variable?
A Marks[3,1] ← Score
B Score ← Marks[3,1]
4. Which IF statement correctly compares two elements of a 2D array?
A IF Marks[1] > Marks[2] THEN
B IF Marks[1,2] > Marks[2,2] THEN
5. Marks[2,1] holds 65 and Marks[2,2] holds 70. What does OUTPUT Marks[2,2] show?
A 70
B 65

Match the Pairs

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

Left columnRight column
1OUTPUT NoughtsAndCrosses[2,3] ADisplays the value stored at row 2, column 3 of the NoughtsAndCrosses array
2Winner ← Marks[4,2] BIF Marks[1,2] > Marks[3,2] THEN
3Compare two elements of a 2D array in an IF statement COUTPUT Marks[3,1]
4Read a value from a table of marks: row 3 is the third student, column 1 is Maths DCopies the value at row 4, column 2 of Marks into the variable Winner
5OUTPUT NoughtsAndCrosses[2][3] EThe row and column are swapped. To show row 2, column 3 the indexes must be written in the order [2,3], row first and then column.
6OUTPUT NoughtsAndCrosses[3,2] FSeparate brackets are used for the indexes. In Cambridge pseudocode both indexes go in one pair of square brackets, separated by a comma: NoughtsAndCrosses[2,3].

Definitions

Name[row, column]

Fill in the Blanks

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

1.
______ NoughtsAndCrosses[2,3]
2.
// Copy row 3, column 2 into Score Score ← Marks[3,_]
3.
IF Marks[1,2] _ Marks[2,2] THEN OUTPUT Marks[1,2] ENDIF
4.
Top ← Marks[2,3] OUTPUT ___
5.
// Show the mark in row 3, column 1 OUTPUT Marks[_,1]

Spot the Error

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

Question 1
A Board[2,3] ← "X"
B Board[1,1] ← "O"
C OUTPUT Board(2,3)
Question 2
A // Show row 3, column 2
B OUTPUT Marks[2,3]
C OUTPUT Marks[1,1]
Question 3
A // Copy Marks[2,2] into Score
B Score ← 0
C Marks[2,2] ← Score
Question 4
A Marks[1,1] ← 60
B Marks[1,2] ← 72
C IF Marks[1,1] < Marks[1,2]
D THEN OUTPUT Marks[1]
E ENDIF
Question 5
A Board[1,1] ← "X"
B Board[2,2] ← "X"
C IF Board[1,1] ← Board[2,2]
D THEN OUTPUT "Win"
E ENDIF

Answer Key

Reading a value from a 2D array element
Section A: Quick Questions
Q1
A — OUTPUT NoughtsAndCrosses[2,3]
Q2
A — The value at row 1, column 2
Q3
B — Score ← Marks[3,1]
Q4
B — IF Marks[1,2] > Marks[2,2] THEN
Q5
A — 70
Section B: Match the Pairs
Q1
OUTPUT NoughtsAndCrosses[2,3] → Displays the value stored at row 2, column 3 of the NoughtsAndCrosses array
Q2
Winner ← Marks[4,2] → Copies the value at row 4, column 2 of Marks into the variable Winner
Q3
Compare two elements of a 2D array in an IF statement → IF Marks[1,2] > Marks[3,2] THEN
Q4
Read a value from a table of marks: row 3 is the third student, column 1 is Maths → OUTPUT Marks[3,1]
Q5
OUTPUT NoughtsAndCrosses[2][3] → Separate brackets are used for the indexes. In Cambridge pseudocode both indexes go in one pair of square brackets, separated by a comma: NoughtsAndCrosses[2,3].
Q6
OUTPUT NoughtsAndCrosses[3,2] → The row and column are swapped. To show row 2, column 3 the indexes must be written in the order [2,3], row first and then column.
Section C: Definitions
Q1
Name[row, column] — Gives the value stored at that row and column of a 2D array. Use it in an expression or OUTPUT, e.g. OUTPUT NoughtsAndCrosses[2,3] shows the value at row 2, column 3.
Section D: Fill in the Blanks
Q1
OUTPUT NoughtsAndCrosses[2,3]
Q2
// Copy row 3, column 2 into Score Score ← Marks[3,2]
Q3
IF Marks[1,2] > Marks[2,2] THEN OUTPUT Marks[1,2] ENDIF
Q4
Top ← Marks[2,3] OUTPUT Top
Q5
// Show the mark in row 3, column 1 OUTPUT Marks[3,1]
Section E: Spot the Error
Q1
C — Array indexes use square brackets, not round ones. It should be OUTPUT Board[2,3].
Q2
B — The row comes first and the column second. Row 3, column 2 is Marks[3,2], not Marks[2,3].
Q3
C — To copy an element into a variable, the variable goes on the left of the arrow: Score ← Marks[2,2]. The broken line overwrites the array element instead.
Q4
D — A 2D array element needs both a row and a column. It should be OUTPUT Marks[1,2].
Q5
C — An IF compares values with =, not the assignment arrow. It should be IF Board[1,1] = Board[2,2].