Example: Multidimensional Arrays for Library Occupancy

Example: A library records occupancy on three floors during three time slots. A multidimensional array arranges entries so that more than one index identifies an element. In this Python example, you represent a two-dimensional array using a list of row lists.


Python
occupancy = [
    [14, 18, 11],
    [9, 16, 13],
    [12, 10, 17]
]


Rows correspond to floors 1, 2, and 3. Columns correspond to morning, afternoon, and evening, in that order. Every entry is a count of people.


Python uses zero-based indexing: the first position has index 0. Therefore, floor 1 corresponds to row 0, and the morning corresponds to column 0. Access an entry with occupancy[row][column]: select the row first, then the entry within that row.


A three by three array shows library occupancy with row and column indices.


a) Write an expression that retrieves the afternoon occupancy on floor 2, and state its value.

  1. Floor 2 has row index ________ because floor 1 starts at index 0.
  2. Afternoon has column index ________ because morning starts at index 0.
  3. Select the second row, [9, 16, 13], and then its second entry.


Python
occupancy[1][1]


The afternoon occupancy on floor 2 is 16 people.


b) Staff discover that the morning count on floor 3 omitted four people. Write a statement that corrects that entry, and state the corrected count.

  1. Floor 3 corresponds to row index 2, and morning corresponds to column index 0. The original entry is ________.
  2. Add the four omitted people to that stored count.


12 people+4 people=16 people.12\text{ people}+4\text{ people}=16\text{ people}.


  1. Assign the result to the same array location.


Python
occupancy[2][0] = occupancy[2][0] + 4


The corrected morning occupancy on floor 3 is 16 people. The other entries retain their previous values.


c) After the correction, calculate the total afternoon occupancy across all three floors using a loop.

  1. Afternoon remains column 1. The correction changed column 0, so the afternoon counts are still 18, 16, and 10 people.
  2. Start a total at zero. Visit each row and add its entry at column 1.


Python
afternoon_total = 0
for floor in occupancy:
    afternoon_total += floor[1]


  1. The running total is 18 people after the first row and ________ after the second row. Adding the third row completes the total.


18+16+10=44 people.18+16+10=44\text{ people}.


The total afternoon occupancy is 44 people.



A shop records item counts in a multidimensional array represented by Python lists. Rows represent shelves, and columns represent product types. What does this code print?


Python
stock = [[23, 31, 27], [18, 26, 34]]
print(stock[1][0])
Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A bus company stores stop identifiers in a Python nested list, a list whose elements are themselves lists. Each inner list represents one route.


Python
routes = [
    [104, 108, 112],
    [205],
    [301, 303]
]


Complete the missing loop bounds as exact integers. A loop visiting every route needs iterations. When the outer index is 1, a loop visiting every stop on that route needs iteration.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A delivery company stores box counts in a multidimensional array represented by Python lists. Each row represents one branch; the three columns represent Monday, Tuesday, and Wednesday, respectively.


Python
deliveries = [[6, 4, 5], [3, 8, 2], [7, 1, 9]]

wednesday_total = 0
for branch in deliveries:
    wednesday_total += branch[2]

second_branch_total = 0
for count in deliveries[1]:
    second_branch_total += count


Give the final values of wednesday_total and second_branch_total, both in boxes, as exact integers.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors