# 09 - Arrays 2d and 3d

1. _________ are data structures that store data under a single identifier by index. Typically, the definition of ________is that they can only hold data items of the same data type, although python lists don't work this way!

Queues

Arrays

Stacks

2. The following array 'a' is multidimensional in nature. Can you predict the output?
```a = [[1, 2, 3, 4], [5, 6], [7, 8, 9]]
print(a[1:2])```

[[2, 3]]

[[1, 2]]

[[5, 6]]

[[5, 7]]

3. To process 2-dimensional arrays, you could use nested loops. The first loop iterates through the _____________, the second loop runs through the elements ______________
```a = [[1, 2, 3, 4], [5, 6], [7, 8, 9]]
for i in range(len(a)):
for j in range(len(a[i])):
print(a[i][j], end=' ')
print()```

column number / outside of the row

row number / inside of the row

column number / inside of the row

row number / in the corresponding column

4. Can you predict the output of the following program that makes use of a 2d array (you could call this a 2d list in python)
```a = [[1, 2, 3, 4], [5, 6], [7, 8, 9]]
s = 0
for i in range(len(a)):
for j in range(len(a[i])):
s += a[i][j]
print(s)```

1

9

1,2,3,4,5,6,7,8,9

45

5. A practical application for 2-dimensional arrays would be to use them to store the available seats in a cinema. What is the output of the following?
```cinema = []

for j in range(5):
column = []
for i in range(3):
column.append(0)
cinema.append(column)

print(cinema)```

[[0, 0, 0], [0, 0, 0], [0, 0, 0],

[[0, 0, 0, 0, 0, 0]]

{[[0, 0, 0]]], [[[0, 0, 0]]], [[[0, 0, 0]]],

[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

6. Analyse the code below which produces a matrix of numbers. Which one of the outputs is produced on execution?
```n = 4
a = [[0] * n for i in range(n)]
for i in range(n):
for j in range(n):
if i < j:
a[i][j] = 0
elif i > j:
a[i][j] = 2
else:
a[i][j] = 1
for row in a:
print(' '.join([str(elem) for elem in row]))

#output 1

2 1 0 0
2 1 0 0
2 2 1 0
2 2 2 1

#output 2

1 0 0 0
2 1 0 0
2 2 1 0
2 2 2 1

#output 3
0 0 0 0
2 1 0 0
2 2 1 0
2 2 2 1

#output 4
1 2 1 2
2 1 0 0
2 2 1 0
2 2 2 1```

Output 3

Output 2

Output 1

Output 4

7. In Python a 2x2 array is [[1,2],[3,4]] with ...

list [3,4] representing the second row and the list [1,2] representing the first row.

[[1,2] representing the columns and [3,4]] representing the rows

list [1,2,3,4] representing the whole 2d array

list [1,2] representing the first row and the list [3,4] representing the second row.

8. The following makes use of 'numpy'. Regardless of whether you have used it, consider what is been shown below.
```>>> c = np.array( [[[  0,  1,  2],
...                 [ 10, 12, 13]],
...                [[100,101,102],
...                 [110,112,113]]])```

a 2D array (two stacked 1D arrays)

a 3D array (three stacked 1D arrays)

a 2D array (two stacked 2D arrays)

a 3D array (two stacked 2D arrays)

9. In a 2-dimensional array: imagine you have rows and columns. The rows are indicated as the axis 0 , while the c_______ are the axis 1. The number of the axis goes ____accordingly with the number of the dimensions

columns / up

columns / down

rows / up

rows / down

10. Below is the representation of a 3d array in python. Can you predict the output?
```matrix3=[[[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]],
[[15,16,17,18,19,20,21],[22,23,24,25,26,27,28]]]
print(matrix3[1][1][2])```

24

17

2

Index out of range Error