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.
中文
行与列
- 二维数组是一张网格。声明时给出两个方向的范围。
- 两个下标用逗号隔开,选出一个格子:先行,后列。
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.
中文
一个循环套在另一个里面
- 外层循环走每一行。内层循环走这一行的每一列。
NEXT写出它所结束的那个循环的计数器。
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],中间有逗号。两个下标都从声明的下界开始。 - 内层的
NEXT写在外层的NEXT之前。
English
Now you try
- Read one cell of a 2-D array, then total the grid.
中文
现在你来试
- 读出二维数组的一格,再把整张网格相加。
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。在第 1 行第 2 列存入 7,并输出这一格。
Click Run to see the output here. · 点击“运行”查看此处输出。
The grid is filled for you. Total all four cells and output the total (10). · 网格已为你填好。把四个格子相加,并输出总和(10)。
Click Run to see the output here. · 点击“运行”查看此处输出。