Preview

02 - 1d 2d 3d Arrays

 1. An array data structure is a data structure consisting of a ___________(values or variables), each identified by at least one array index or key

  single binary value

  stack of stacks

  set of binary sequences

  collection of elements

 2. The definition of an 'array' typically stands to mean a collection of data items of ____________
Note: It is worth noting that Python uses lists (unless you use numpy arrays). A list can have items of different data types.

  the same data type

  the same numeric value

  the same binary pattern

  a completely different data type

 3. In the following example, what would be returned (in python) by print(humans[2])
humans=["Adam", "Eve", "Cain", "Abel","Seth"]


0      1     2     3     4
=============================
Adam  Eve   Cain  Abel  Seth
=============================

  Adam

  Seth

  Cain

  Eve

 4. Using the language VB.Net, if this 2d array was called 'matrix', which of the following values would locate the value of the 'x' on the grid.
Visualising a 2d Array
-----------------------


0      1     2     3     4
=============================
   |
1  |    
   |-------------------------
   |    
2  |               x
   |--------------------------
3  |
   |
   |--------------------------
4  |
   |
    ---------------------------

  matrix(2,2)

  matrix(3,3)

  matrix(0,3)

  matrix(2,3)

 5. It is helpful to visualise a 2d array as a table with a _________________________

  at least four rows

  border

  height and width

  height of at least 2 cm

 6. 3d arrays could be visualised or imagined as a _________ with height, width and _______.

  cube / depth

  rectangle / height x 2

  square / additional width

  cube / additional columns (width)

 7. In Python, a multidimensional list is a __________________________

  variable inside a variable

  list within a list (or a list within a list within a list!)

  table row inside a column

  list within a matrix

 8. Analyse the image and information below. What would the output of this print call be?
2darray_printcall_1.png

  1

  3

  2

  4

 9. State the output of this code snippet. Note the print call: print(matrix[1][2])
matrix=[[1,2,3,4],[5,7,8,9]]
print(matrix[1][2])

  6

  8

  List index out of range

  7

 10. What is the output of this code snippet. Note the print call: print(matrix[2][2])
matrix=[[1,2,3,4],[5,7,8,9]]
print(matrix[2][2])

  7

  8

  6

  List index out of range

 11. Analyse the image below which shows a 3d list. What is the output?
3darrayoutput.png

  23

  24

  6

  18

 12. This code shows looping through a 1d list. In the output, what room is Maths allocated to?
subjects = ['Biology', 'Chemistry', 'Physics', 'Maths', 'Computing', 'English']

for i in range(len(subjects)):
    print('Room[',i,'] =', subjects[i])

  3

  2

  4

  1

 13. In the following code ________________ have been used to calculate the output which is ___________.
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)

  two nested loops / 1,2,3,4

  three loops / 9

  two nested loops / 45

  a single for loop / 1

 14. In the following excerpt, try and understand the concept and fill in the blanks.
Here is a square array
========================
an array 'a' of n rows and n columns.

The main diagnal has been set to 1.

1 8 8 8
9 1 8 8
9 9 1 8
9 9 9 1

Those elements above the following:
<a[i][j] for which i==j>

are set to _______

  i or j

  8

  1

  9

 15. Can you fill in the blanks after analysing the question?
An array 'a' of n rows and n columns.

The main diagnal has been set to 1.

1 8 8 8
9 1 8 8
9 9 1 8
9 9 9 1

Note: The elements that lie _____ the 
main diagonal – are elements a[i][j] 
for which i<j

  to the left hand side of

  below

  directly next to

  above

 16. The following (optional to go through) powerpoit provides a scenario for a laser tag game. Which statement is true?
VB.Net
=======
Note: You should be able to follow the 
logic even if you have not coded in VB.Net

A 2d array called players is declared
as follows:

Dim players(3,1)

This will give us a ________________________

  3 x 3 grid

  3 x 11 grid

  2 x 4 grid

  1 x 3 grid

 17. If the user entered '4' for rows and '2' for columns, what would the output be of the following code?
row_num = int(input("Input number of rows: "))
col_num = int(input("Input number of columns: "))
multi_list = [[0 for col in range(col_num)] for row in range(row_num)]

for row in range(row_num):
    for col in range(col_num):
        multi_list[row][col]= row*col

print(multi_list)

  [[0, 0], [0, 1], [0, 2], [0, 3]]

  [[0,1], [0, 1], [0, 3], [0, 4]]

  [[0, 0], [1, 1], [2, 2], [3, 3]]

  [[1, 0], [2, 1], [3, 2], [0, 3]]

 18. The following code 'pops' (removes) an item from the array and returns a result. What is the output?
from array import *
array_num = array('i', [1, 3, 5, 7, 9])
print("Original array: "+str(array_num))
array_num.pop(2)
print("New array: "+str(array_num))

  New array: array('i', [1, 3, 7, 9])

  New array: array('i', [1, 5, 7, 9])

  New array: array('i', [3, 5, 7, 9])

  New array: array('i', [1, 3, 5, 9])

 19. Numpy is a scientific package used with Python and contains a powerful 'n' array object. Fill in the blanks for y.
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6])
print("6 rows and 0 columns")
print(x.shape)

y = np.array([[1, 2, 3],[4, 5, 6],[7,8,9]])
print("(3, 3) -> _________________________ ")
print(y)

x = np.array([1,2,3,4,5,6,7,8,9])
print("Change array shape to (3, 3) -> 3 rows and 3 columns ")
x.shape = (3, 3)
print(x)

  3 columns and 0 rows

  3 rows and 0 columns

  3 rows and 3 columns

  6 rows and 3 columns

 20. Because the mathematical concept of a matrix can be represented as a two-dimensional grid, two-dimensional arrays are _________________

  also called 'Keanu Reeves'

  far easier to code than a 1d array

  now obsolete and no longer used

  also called matrices