Night 23: A map for your pet
You already finished this night, in Python or pseudocode. It counts for both. Practise again if you like.
Tonight: a map for your pet
- Your pet's world needs a map: rows and columns, like a chess board.
- You will learn one thing: 2D arrays.
Words for tonight
| Word | Meaning |
|---|---|
| grid | rows and columns of boxes |
| row | a line from left to right |
| column | a line from top to bottom |
Two indexes
- A 2D array has a range for the rows and a range for the columns.
World[Row, Column]: first the row, then the column. Both start at 1.
DECLARE World : ARRAY[1:2, 1:3] OF STRING
World[1, 1] ← "tree"
World[1, 2] ← "grass"
World[1, 3] ← "pond"
World[2, 1] ← "grass"
World[2, 2] ← "home"
World[2, 3] ← "grass"
OUTPUT World[2, 2]
OUTPUT World[1, 3]

Fill and print the map
- Two loops can fill every place, and two more can print it.
DECLARE Grid : ARRAY[1:2, 1:3] OF STRING
DECLARE RowNumber, Col : INTEGER
DECLARE Row : STRING
FOR RowNumber ← 1 TO 2
FOR Col ← 1 TO 3
Grid[RowNumber, Col] ← "."
NEXT Col
NEXT RowNumber
Grid[2, 3] ← "P"
FOR RowNumber ← 1 TO 2
Row ← ""
FOR Col ← 1 TO 3
Row ← Row & Grid[RowNumber, Col]
NEXT Col
OUTPUT Row
NEXT RowNumber
Rows first
| Code | Means |
|---|---|
Grid[2, 3] |
row 2, column 3 |
Grid[3, 2] |
row 3, column 2: a different place! |
- In exams, the question always says which index is the row. Read it carefully.
- Now draw your pet's map!
Difficulty 2/5 · 140 coins + a small bonus on first completion today
Here is your pet's world as a 2D array. Output the place at row 2, column 2, then the place at row 1, column 3. The output is home, then pond.
Click Run to see the output here.
Difficulty 2/5 · 140 coins + a small bonus on first completion today
Put your pet P at row 3, column 1. Then output each row as one line, built with &. The last line is P...
Click Run to see the output here.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Count the trees T in the garden with a loop inside a loop. Output the count: 3.
Click Run to see the output here.
Difficulty 4/5 · 280 coins + a small bonus on first completion today
Find where H (home) is. Use two loops and output its row and column: 2 3.
Click Run to see the output here.