Night 23: A map for your pet
Vous avez déjà terminé cette soirée, en Python ou en pseudocode. Elle compte pour les deux. Entraînez-vous à nouveau si vous le souhaitez.
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!
Difficulté 2/5 · 140 pièces + un petit bonus lors de la première réussite aujourd'hui
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.
Cliquez sur Exécuter pour voir le résultat ici.
Difficulté 2/5 · 140 pièces + un petit bonus lors de la première réussite aujourd'hui
Put your pet P at row 3, column 1. Then output each row as one line, built with &. The last line is P...
Cliquez sur Exécuter pour voir le résultat ici.
Difficulté 3/5 · 200 pièces + un petit bonus lors de la première réussite aujourd'hui
Count the trees T in the garden with a loop inside a loop. Output the count: 3.
Cliquez sur Exécuter pour voir le résultat ici.
Difficulté 4/5 · 280 pièces + un petit bonus lors de la première réussite aujourd'hui
Find where H (home) is. Use two loops and output its row and column: 2 3.
Cliquez sur Exécuter pour voir le résultat ici.