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.
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) Write an expression that retrieves the afternoon occupancy on floor 2, and state its value.
- Floor 2 has row index ________ because floor 1 starts at index 0.
- Afternoon has column index ________ because morning starts at index 0.
- Select the second row, [9, 16, 13], and then its second entry.
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.
- Floor 3 corresponds to row index 2, and morning corresponds to column index 0. The original entry is ________.
- Add the four omitted people to that stored count.
- Assign the result to the same array location.
occupancy[2][0] = occupancy[2][0] + 4The 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.
- Afternoon remains column 1. The correction changed column 0, so the afternoon counts are still 18, 16, and 10 people.
- Start a total at zero. Visit each row and add its entry at column 1.
afternoon_total = 0
for floor in occupancy:
afternoon_total += floor[1]- The running total is 18 people after the first row and ________ after the second row. Adding the third row completes the total.
The total afternoon occupancy is 44 people.
A bus company stores stop identifiers in a Python nested list, a list whose elements are themselves lists. Each inner list represents one route.
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.
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.
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 += countGive the final values of wednesday_total and second_branch_total, both in boxes, as exact integers.