Writing a value into a 2D array element

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

Writing a value into a 2D array element

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line stores 'X' in row 2, column 3 of NoughtsAndCrosses?
A NoughtsAndCrosses[2,3] ← 'X'
B NoughtsAndCrosses[3,2] ← 'X'
2. Which statement stores a value in a 2D array element correctly?
A Board[1] ← 'O'
B Board[1,1] ← 'O'
3. Which line correctly stores a CHAR value in a 2D array?
A Grid[1,2] ← "X"
B Grid[1,2] ← 'X'
4. In Board[3,1] ← 'O', which element receives the value?
A Row 3, column 1
B Row 1, column 3
5. After Grid[1,3] ← 'X' and Grid[3,1] ← 'X', how many different elements have been changed?
A Two
B One

Match the Pairs

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

Left columnRight column
1NoughtsAndCrosses[2,3] ← 'X' ANoughtsAndCrosses[1,1] ← 'O'
2NoughtsAndCrosses[3,2] ← 'O' BStores the CHAR 'O' in row 3, column 2, a different element from row 2, column 3
3Write a CHAR value into the element in row 1, column 1 of a 2D array (the CHAR goes in single quotes) CGrid[3,1] ← 'X'
4Write a value into one element, giving the row index first and then the column index DStores the CHAR 'X' in the element in row 2, column 3 of the 2D array
5NoughtsAndCrosses[2] ← 'X' EA 2D array needs two index values, a row and a column. Only one index is given, so no single element is identified.
6NoughtsAndCrosses[1,2] ← "X" FThe CHAR value is in double quotes. A CHAR must be written in single quotes, and double quotes are for STRING values.

Definitions

Name[row, column] ← value

Fill in the Blanks

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

1.
NoughtsAndCrosses[_,3] ← 'X'
2.
NoughtsAndCrosses[2,_] ← 'X'
3.
_________________[1,1] ← 'O'
4.
NoughtsAndCrosses[2,3] ← 'X' // Put an O in row 3, column 2 NoughtsAndCrosses[_,2] ← 'O'
5.
NoughtsAndCrosses[1,1] _ 'O'

Spot the Error

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

Question 1
A Board[1,1] ← "X"
B Board[2,2] ← 'O'
C Board[3,3] ← 'X'
Question 2
A // X goes in row 2, column 3
B NoughtsAndCrosses[3,2] ← 'X'
C OUTPUT NoughtsAndCrosses[2,3]
Question 3
A Score[1,1] ← 12
B Score[1,2] ← 15
C Score[2,1] ← 9
D Score[2,2] = 14
Question 4
A Board[1][2] ← 'O'
B Board[2,2] ← 'X'
C OUTPUT Board[1,2]
Question 5
A Board[1,1] ← 'X'
B Board[1,2] ← 'O'
C Board[1,3] ← 'X'
D Board[2,1] ← O

Answer Key

Writing a value into a 2D array element
Section A: Quick Questions
Q1
A — NoughtsAndCrosses[2,3] ← 'X'
Q2
B — Board[1,1] ← 'O'
Q3
B — Grid[1,2] ← 'X'
Q4
A — Row 3, column 1
Q5
A — Two
Section B: Match the Pairs
Q1
NoughtsAndCrosses[2,3] ← 'X' → Stores the CHAR 'X' in the element in row 2, column 3 of the 2D array
Q2
NoughtsAndCrosses[3,2] ← 'O' → Stores the CHAR 'O' in row 3, column 2, a different element from row 2, column 3
Q3
Write a CHAR value into the element in row 1, column 1 of a 2D array (the CHAR goes in single quotes) → NoughtsAndCrosses[1,1] ← 'O'
Q4
Write a value into one element, giving the row index first and then the column index → Grid[3,1] ← 'X'
Q5
NoughtsAndCrosses[2] ← 'X' → A 2D array needs two index values, a row and a column. Only one index is given, so no single element is identified.
Q6
NoughtsAndCrosses[1,2] ← "X" → The CHAR value is in double quotes. A CHAR must be written in single quotes, and double quotes are for STRING values.
Section C: Definitions
Q1
Name[row, column] ← value — Stores a value in one element of a 2D array. Give the row index first, then the column index, e.g. NoughtsAndCrosses[2,3] ← 'X' puts a CHAR in row 2, column 3.
Section D: Fill in the Blanks
Q1
NoughtsAndCrosses[2,3] ← 'X'
Q2
NoughtsAndCrosses[2,3] ← 'X'
Q3
NoughtsAndCrosses[1,1] ← 'O'
Q4
NoughtsAndCrosses[2,3] ← 'X' // Put an O in row 3, column 2 NoughtsAndCrosses[3,2] ← 'O'
Q5
NoughtsAndCrosses[1,1] ← 'O'
Section E: Spot the Error
Q1
A — A CHAR value must be written in single quotes. Double quotes make it a STRING. Line 1 should be Board[1,1] ← 'X'.
Q2
B — The row index comes first and the column index second. Row 2, column 3 is [2,3], not [3,2], which is a different element.
Q3
D — A value is stored in an array element with the assignment arrow ←. A single = is a comparison in pseudocode. It should be Score[2,2] ← 14.
Q4
A — A 2D array element uses one pair of brackets with both indexes separated by a comma. It should be Board[1,2] ← 'O'.
Q5
D — The CHAR value O has no quotes, so it looks like a variable name. A CHAR needs single quotes: Board[2,1] ← 'O'.