2-D arrays · 2次元配列
A 2-D array is a grid
- A 2-D array has rows and columns, like a table.
ARRAY[1:2, 1:3]means 2 rows and 3 columns.
2次元配列はグリッド
- 2次元配列は行と列を持ち、表のようなものです。
ARRAY[1:2, 1:3]は2行3列を意味します。
DECLARE G : ARRAY[1:2, 1:3] OF INTEGER
G[1,1] ← 5
OUTPUT G[1,1] // → 5
Two indexes: row, column
- Use
G[row, column]to reach one cell. - The first cell is
G[1,1].
2つのインデックス:行、列
G[row, column]を使って特定のセルにアクセスします。- 最初のセルは
G[1,1]です。
DECLARE G : ARRAY[1:2, 1:2] OF INTEGER
G[1,2] ← 7
G[2,1] ← 9
OUTPUT G[1,2] // → 7
Nested FOR over a grid
- An outer loop walks the rows; an inner loop walks the columns.
グリッドに対するFORループのネスト
- 外側のループは行を、内側のループは列を巡ります。
DECLARE G : ARRAY[1:2, 1:3] OF INTEGER
DECLARE R, C : INTEGER
FOR R ← 1 TO 2
FOR C ← 1 TO 3
G[R, C] ← R * C
NEXT C
NEXT R
FOR R ← 1 TO 2
FOR C ← 1 TO 3
OUTPUT G[R, C]
NEXT C
NEXT R
Watch out
- The order is
G[row, column]— row first, then column. NEXT R
注意
- 順序は
G[row, column]——まず行、次に列です。 NEXT R
Common mistakes
- A 2-D array is
ARRAY[1:r, 1:c]; use two indexes. - The outer loop walks the rows, the inner the columns.
よくあるミス
- 2次元配列は
ARRAY[1:r, 1:c]で、2つのインデックスを使います。 - 外側のループは行を、内側のループは列を巡ります。
Now you try
- Use two indexes, or nested
FORloops. - Press Check answer to test it.
あなたも試してみよう
- インデックスを2つ使うか、
FORのループを嵌套(ネスト)します。 - Check answer を押して確認する。
Declare a 2×2 grid G. Store 1 in G[1,1] and 4 in G[2,2], then output their sum (it is 5). · 2×2のグリッド Gを宣言する。1をG[1,1]に、4をG[2,2]に格納し、その和を出力する(値は5である)。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Fill a 2×3 grid G so each cell holds row × column, then add up every cell with nested loops and output the total (it is 18). · 2×3のグリッド Gを埋め、各セルにrow × columnを入れる。次に、ネストループで全てのセルを加算し、合計を出力する(値は18である)。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Use a loop to output a 3-by-3 square of stars: three lines, each with three *. OUTPUT takes several values separated by commas. · ループを使って星の 3 × 3 の正方形を出力してください。3行あり、各行に3つの * があります。OUTPUT はコンマで区切られた複数の値を取ります。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。