2-D arrays
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
English
Rows and columns
- A two-dimensional array is a grid. The declaration gives both bounds.
- Two indexes, separated by a comma, pick a cell: row, then column.
ไทย
แถวและคอลัมน์
- Two-dimensional array คือตาราง. การประกาศจะกำหนดทั้งสองขอบเขต
- ใช้สอง indexes แยกด้วยเครื่องหมายคั่นเพื่อเลือกเซลล์: แถวก่อน คอลัมน์หลัง
English
ไทย
DECLARE Grid : ARRAY[1:2, 1:2] OF INTEGER
Grid[1, 1] ← 4
Grid[1, 2] ← 7
OUTPUT Grid[1, 2]
English
One loop inside another
- The outer loop walks the rows. The inner loop walks the columns of that row.
NEXTnames the counter of the loop it closes.
ไทย
ลูปหนึ่งซ้อนอีกลูปหนึ่ง
- Outer loop เดินผ่านแถว. Inner loop เดินผ่านคอลัมน์ของแถวนั้น
NEXTชื่อบinary counter ของลูปที่มันปิด
DECLARE Grid : ARRAY[1:2, 1:2] OF INTEGER
Grid[1, 1] ← 1
Grid[1, 2] ← 2
Grid[2, 1] ← 3
Grid[2, 2] ← 4
DECLARE Total : INTEGER
Total ← 0
FOR Row ← 1 TO 2
FOR Column ← 1 TO 2
Total ← Total + Grid[Row, Column]
NEXT Column
NEXT Row
OUTPUT Total
English
Common mistakes
- Write
Grid[1, 2], with a comma. Both indexes start at the declared lower bound. - The inner
NEXTcomes before the outer one.
ไทย
ข้อผิดพลาดที่พบบ่อย
- เขียน
Grid[1, 2]พร้อมคั่นด้วยจุลภาค ดัชนีทั้งสองเริ่มต้นจากขอบเขตล่างที่กำหนดไว้ - Inner
NEXTอยู่ด้านหน้า outer loop
English
Now you try
- Read one cell of a 2-D array, then total the grid.
ไทย
ลองดูเลย
- อ่านเซลล์หนึ่งของอาร์เรย์ 2-D แล้วรวมค่าทั้งหมดในตาราง
Declare Grid as ARRAY[1:2, 1:2] OF INTEGER. Store 7 in row 1, column 2, and output that cell. · ประกาศ Grid เป็น ARRAY[1:2, 1:2] OF INTEGER. เก็บ 7 ในแถว 1, คอลัมน์ 2, และเอาต์พุตเซลล์นั้น
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
The grid is filled for you. Total all four cells and output the total (10). · ตารางถูกเติมให้แล้ว. รวมทุกสี่เซลล์และเอาต์พุตผลรวม (10)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่