Objectives

Students will be able to:

  • understand the concept of 2D array.
  • create 2-D arrays using different techniques
  • process 2-D arrays using nested loops

Two Dimensional Arrays

Drivers for a racing team complete test circuits. They complete 3 test circuits every day from Monday to Friday. The team monitors how many times a driver uses the in-car radio to report a problem. This data is stored and analysed. Here is a sample table of data:

 

We can see that the data stored has the same data type - INTEGER. So, to help us manage the data we should use an array. We can also see that the data is easily understandable in a tabular format - we can imagine a table or a spreadsheet would be useful to store the data. In many computer programming languages, including Python, we can create a 2-dimensional array data structure to store this data.

2-dimensional arrays have a number of rows and a number of columns. In the above example, there are 5 rows and each row has 3 columns.

In Python, there are several ways to create and populate a 2-dimensional array. Let's populate a 2-dimensional array of 5 rows and 3 columns with zeroes like this:

 

   Technique 1

A 2D array is really just an array with lots of arrays inside it!

So, to create a 2D array in Python, we can do this:

radioCalls = [[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]

To make it easier to visualize, we could change the layout of the code to:

radioCalls = [

[0,0,0],

[0,0,0],

[0,0,0],

[0,0,0],

[0,0,0]

]

   Technique 2

Create a single array with 3 elements. Put a zero into each element.

arr = [0,0,0]

Next create an empty array.

twoDArr = []

Now, append 5 of the single arrays into the empty array - after all, a 2D array is just an array of arrays!

for i in range(5):

twoDArr.append(arr)

So how do we access each of the individual elements in a 2D array?

We can assign values to each element of the array as follows:

radioCalls[0][0] = 3;
radioCalls[0][1] = 2;
radioCalls[0][2] = 1;
 

As you can see, the first index refers to the row and the second index refers to the column.

So, how can we process individual elements of an array?

A classic technique is to use a nested count-controlled loop. The outer loop processes the rows and the inner loop processes the columns:

for i in range(len(radioCalls)):#note len(radioCalls) tells us the number of rows

for j in range(len(radioCalls[i])):#note len(radioCalls[i]) tells us the number of columns in the row being processed

print(radioCalls[i][j])

Do you think you could write some code to total up all of the values stored in the 2-dimensional array?

Food For Thought

2-dimensional arrays are very useful and any Computer Scientist should know how to use them.

One important thing to consider is that a 2D array is just an array of 1D arrays!




tags

row column integer element count-controlled ascending index nested descending identifier datatype


SUBSCRIBE

Join my mailing list to receive updates on the latest blog posts and other things.