Lesson 2 Two Dimensional Arrays
Lesson 2 Two Dimensional Arrays
Two-dimensional arrays
Two dimensional arrays
• A one-dimensional array can be seen as data elements
organised in a row. A two-dimensional array can be
visualised as a grid (or table) with rows and columns.
• For example, a nine-by-nine grid could be referenced with
numbers for each row and letters for each column. A
nine-by-nine, two-dimensional array could be declared
with a statement such as:
game [9][9]
• Many games use two dimensional arrays to plot the visual
environment of a game. Positions in a two dimensional
array are referenced like a map using horizontal and
vertical reference numbers (known as matrices).
• We can also make three-dimensional arrays as well. Many
games (such as Minecraft) use three-dimensional arrays
to model an environment.
Creating a two-dimensional array
• To create a two-dimensional nine-by-nine array in Python, we would use code such
as:
game= [[0 for x in range (9)] for y in range(9)]
• This statement contains two FOR loops and each element is initialised in turn.
• We can now use statements like the following to add data to positions in the array:
game[0][0] = '3'
game[0][1] = '3'
game[0][2] = '3'
game[1][0] = '2'
game[1][1] = '2'
Our array would now look like this:
Task 1 – Complete the array
The following array is created using python;
[3]
Writing 2D Arrays/Lists in Python
• To begin, a list is declared in the same way as a normal list. Next, you need to create another
array. This is done by adding a comer and creating another list of data. Note how both levels of
the array are wrapped in additional square brackets;
my_list = [[1, 3, 5, 7, 9, 11, 13, 15],[2, 4, 6, 8, 10, 12, 14,
16]]
• This array essentially can be considered to look like this;
index 0 1 2 3 4 5 6 7
0 1 3 5 7 9 11 13 15
1 2 4 6 8 10 12 14 16
To locate a value, first identify the row index followed by the column index, e.g.
[Row index][Column index]
The value of [0][0] is 1
The value of [0][5] is 11
The value of [1][4] is 10
Task 2: Creating 2 dimensional arrays in python
1. Write out the following code. 2. In the table below explain what
the following commands did:
Command Explanation
print (a)
print (a[0])
print (a[1][1])
names =
[[names1],names2]]
Adjust these
numbers to
change the size of
your grid.
Task 3: Array programming techniques - Create grids
Using any of the three techniques shown to create a grid, create the following;
• 6 x 6 grid
• 10 x 10 grid
Evidence your programming and output grids below;
10 x 10 grid 10 x 10 grid
Task 4: Noughts and Crosses (Tic-Tac-Toe)
This function checks every possibility of winning through Boolean checks. If the win checks return
False, the function checks for a draw. If the draw check returns False, the game carries on running.
Part 5 – Run the game
This code runs the game which requires the output produced from the functions we
made earlier. There is one piece of code in particular which is vital to the success of the
game;
if(player % 2 != 0):
This code uses the % symbol. In python this symbol is used to divide two integers to
check for a remainder number.
Remember: integers cannot be converted to floats, e.g. 3 /1 cannot become 1.5, the
returned value would be remainder 1
The purpose of this code is to check whose turn it is. It does this by checking if by
dividing the value of player by 2, will there be a remainder. If the remainder is not equal
to 0 (i.e. there is a remainder), the number must be odd which therefore means its
player 1’s turn, else it must be an even number which means it is player 2’s turn.
It is also used later to check who has won.
The other important code is allowing the user to input their go. Note their input has
been converted to an integer.
Part 5 – Run the game
Part 6 – Check who’s won!
The last section of code only runs if a draw or win has been returned True
from the CheckWin function. Again the % character is used to check for an
odd or even number, representing the winning player.
Evidence – Your game code below
Evidence – Print screen your game running below
Array Exam Questions
Array Exam Questions
1. What is a data structure? 2. What is an array?
a) A list of variables
a) A list of variables
b) A list of strings
b) A list of variables and strings
c) A series of memory locations, each with
c) A facility that holds more than one value
the same name, that hold related data
a) Arrays store multiple data values under one a) The smallest number an array can hold
name, reducing the complexity of our program
b) The biggest number an array can hold
b) Arrays store more data than if we were using
c) An element is one value in an array
variables
c) Arrays store more data than if we were using
strings
Array Exam Questions
5. How many elements would the array 6. In the array 'numbers(6, 10, 7, 12, 9, 2)', what is the
'student_marks(10)' hold? value of element numbers(4)?
7. When creating the array 'scores(9)', what does the 8. What is the difference between an array and a list?
'9' in brackets represent?
a) An array holds numbers, whereas a list holds
a) The number of elements the array can hold strings
b) An array can only hold data of the same
b) The largest number of digits an element can
data type, whereas lists can hold values of
hold
different data types
c) The largest number of characters an element c) An array holds both numbers and strings,
can hold whereas a list only holds numbers
Answers
Answers
1. What is a data structure? 2. What is an array?
a) A list of variables
a) A list of variables
b) A list of strings
b) A list of variables and strings
c) A series of memory locations, each with
c) A facility that holds more than one value
the same name, that hold related data
a) Arrays store multiple data values under one a) The smallest number an array can hold
name, reducing the complexity of our program
b) The biggest number an array can hold
b) Arrays store more data than if we were using
c) An element is one value in an array
variables
c) Arrays store more data than if we were using
strings
Remember: Computers start
Answers counting from 0!
5. How many elements would the array 'scores(9)' 6. In the array 'numbers(6, 10, 7, 12, 9, 2)', what is the
hold? value of element numbers(4)?
7. When creating the array 'scores(9)', what does the 8. What is the difference between an array and a list?
'9' in brackets represent?
a) An array holds numbers, whereas a list holds
a) The number of elements the array can hold strings
b) An array can only hold data of the same
b) The largest number of digits an element can
data type, whereas lists can hold values of
hold
different data types
c) The largest number of characters an element c) An array holds both numbers and strings,
can hold whereas a list only holds numbers