The answersDownload the PDF
Worksheet

F3.5 Two-dimensional arrays

Strings, lists and records · GCSE · OCR J277 2.2.3, AQA 8525 3.2.6, Edexcel 1CP2 6.3.1 · about 15 min

BugBotLab
NameClassDate

What this lesson is about

Rows and columns: the depth grid as a list of lists.

Questions 6 marks in all

  1. [1 mark]What does this program print?

    board = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    print(board[1][2])
  2. [1 mark]What does this program print?

    board = [[1, 2], [3, 4]]
    total = 0
    for row in board:
        for item in row:
            total = total + item
    print(total)
  3. [1 mark]A 2D array has 8 rows and 8 columns. How many items does it hold?

  4. [1 mark]The depth grid comes as one flat list of 64 readings, row after row. Which slice is row 2?

    1. Aflat[16:24]
    2. Bflat[2:10]
    3. Cflat[8:16]
    4. Dflat[16:32]
  5. [1 mark]What does this program print?

    grid = [[0, 0], [0, 0]]
    grid[1][0] = 5
    print(grid)
  6. [1 mark]In OCR's Exam Reference Language, how is row 3, column 4 of grid written?

    1. Agrid[3, 4]
    2. Bgrid[3][4]
    3. Cgrid(3, 4)
    4. Dgrid.3.4

The task: nearest column

There is a box off to one side. Without moving, find the smallest reading in the level rows (2 and 3) of the depth grid and print nearest: <cm> cm in column <n>.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

flat = tof_grid()
best = min(flat)
print("nearest:", best)

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/f3-5-two-dimensional-arrays/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Print the grid with every reading over 100 shown as --, so the shape of the box stands out.
  2. Work out the average of each column in the level rows, and say which side of the robot is more crowded.
  3. Make a 3 by 3 noughts and crosses board of ".", put an X in the middle, and print it as three lines.