2-D arrays
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
แอลเลย์ 2 มิติคือตาราง
- แอลเลย์ 2 มิติ มีแถวและคอลัมน์, เหมือนตาราง
ARRAY[1:2, 1:3]หมายถึง 2 แถวและ 3 คอลัมน์
English
DECLARE Grid : ARRAY[1:2, 1:3] OF INTEGER
Grid[1,1] ← 5
OUTPUT Grid[1,1] // → 5
Two indexes: row, column
- ใช้
Grid[Row, Column]เพื่อเข้าถึงเซลล์หนึ่ง - เซลล์แรกคือ
Grid[1,1]
English
DECLARE Grid : ARRAY[1:2, 1:2] OF INTEGER
Grid[1,2] ← 7
Grid[2,1] ← 9
OUTPUT Grid[1,2] // → 7
Nested FOR over a grid
- L For loop walk the rows; an inner loop walk the columns.
English
DECLARE Grid : ARRAY[1:2, 1:3] OF INTEGER
DECLARE Row, Column : INTEGER
FOR Row ← 1 TO 2
FOR Column ← 1 TO 3
Grid[Row, Column] ← Row * Column
NEXT Column
NEXT Row
FOR Row ← 1 TO 2
FOR Column ← 1 TO 3
OUTPUT Grid[Row, Column]
NEXT Column
NEXT Row
ระวัง
- The order is
Grid[Row, Column]— row first, then column.
ข้อผิดพลาดที่พบบ่อย
- A 2-D array is
ARRAY[1:RowCount, 1:ColumnCount]; use two indexes. - Loop ภายนอกเดินผ่านแถวLoop ภายในเดินผ่านหลัก
ลองดูเลย
- Use two indexes, or nested
FORloops. - กด Check answer เพื่อทดสอบ
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Declare a 2×2 grid Grid. Store 1 in Grid[1,1] และ 4 in Grid[2,2], then output their sum (it is 5).
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Fill a 2×3 grid Grid so each cell holds row × column, then add up every cell with nested loops and output the total (it is 18).
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Use a loop to output a 3-by-3 square of stars: three lines, each with three *. OUTPUT takes several values separated by commas.
คลิก Run เพื่อดูผลลัพธ์ที่นี่