Night 23: A map for your pet
כבר השלמתם את הערב הזה, בפייתון או בפסאודו-קוד. הוא נחשב לשניהם. תוכלו לתרגל שוב אם תרצו.
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!
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
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.
לחץ על הרץ כדי לראות את התוצא כאן.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Put your pet P at row 3, column 1. Then output each row as one line, built with &. The last line is P...
לחץ על הרץ כדי לראות את התוצא כאן.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
Count the trees T in the garden with a loop inside a loop. Output the count: 3.
לחץ על הרץ כדי לראות את התוצא כאן.
רמת קושי 4/5 · 280 מטבעות + בונוס קטן בהשלמה הראשונה היום
Find where H (home) is. Use two loops and output its row and column: 2 3.
לחץ על הרץ כדי לראות את התוצא כאן.