2-D arrays: working with a grid · 2次元配列:グリッドとの操作
A grid of boxes
- A 2-D array is a grid: rows and columns of the same type.
int g[2][3];makes 2 rows and 3 columns — 6 ints in all.- You reach one cell with two indexes:
g[row][col].
箱のグリッド
- 2次元配列はグリッドです。同じ型の行と列で構成されます。
int g[2][3];は2行3列を作成します—合計6個のint。- 特定のセルにアクセスするには2つのインデックスを使います:
g[row][col]。
Rows then columns (row-major)
- C stores a grid row by row: all of row 0, then all of row 1.
- To visit every cell, use a
forloop for the rows, and aforloop inside it for the columns. g[r][c]is the cell in rowr, columnc. Both start at0.
行優先(row-major)
- Cはグリッドを行単位でメモリに格納します:行0の全要素、次に行1の全要素。
- 全セルを処理するには、行に対して
forループを使い、その内部で列に対してforループを使います。 g[r][c]は行r、列cのセルを表します。どちらも0から始まります。
Passing a 2-D array
- When you pass a grid to a function, you must give the column count:
int g[][3]. - C needs the number of columns to find each row in memory; the row count can stay blank.
- So a function takes
int g[][3], int rows— the columns are fixed at3, the rows are passed in.
2次元配列を渡す
- グリッドを関数に渡す際は、列数を指定する必要があります:
int g[][3]。 - Cはメモリ上で各行の位置を特定するために列数が必要ですが、行数は省略可能(空白)です。
- したがって、関数は
int g[][3], int rowsを受け取ります—列数は固定で3、行数は動的に渡されます。
#include <stdio.h>
int main(void) {
int g[2][3] = {{1, 2, 3}, {4, 5, 6}};
int total = 0;
for (int r = 0; r < 2; r++) {
for (int c = 0; c < 3; c++) {
total += g[r][c];
}
}
printf("%d\n", total); // 21
return 0;
}
Working one row at a time
- You can also work on a single row: loop only the columns for a fixed row
r. - This is how you sum one row, or find the biggest value in a row.
- Mixing the two — an outer row loop with an inner column loop — handles the whole grid.
1行ずつ処理する
- 単一行に対しても処理できます:固定された行
rに対して列のみをループさせます。 - これが、ある行の値を合計したり、行内の最大値を見つけたりする方法です。
- 外側の行ループと内側の列ループを組み合わせることで、グリッド全体を処理できます。
Common mistakes
- Index a grid
grid[row][col]. - The outer loop walks the rows, the inner loop the columns.
よくあるミス
- グリッド
grid[row][col]にインデックスを付ける。 - 外側ループは行をトレースし、内側ループは列をトレースします。
Now you try
- For the function tasks, the grid has 3 columns, so the parameter is
int g[][3]. - Do not write a
main— the checker provides one.
あなたも試してみよう
- 関数のタスクでは、グリッドに3列あるので、パラメータは
int g[][3]です。 mainを書かないでください;チェックツールが用意します。
A 2-D array is a grid · 2次元配列はグリッドである
Reach a cell with a[row][col] — row first, then column. · a[row][col] で表されるセルにアクセスする——行を先に指定し、次に列を指定する。
Complete int sum_grid(int g[][3], int rows) so it returns the total of every cell in a grid with rows rows and 3 columns. Do not write a main. · int sum_grid(int g[][3], int rows)を完成させて、rows行3列のグリッドにあるすべてのセルの合計を返すようにする。mainは書かない。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Complete int row_sum(int g[][3], int r) so it returns the total of the three cells in row r. Do not write a main. · int row_sum(int g[][3], int r)を完成させて、行rの3つのセルの合計を返すようにする。mainは書かない。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
The 2×3 grid g is given. In main, print each row with the three numbers separated by single spaces, one row per line. · g という 2×3 グリッドが与えられています。main で、各行を出力し、3つの数字を単一スペースで区切り、1行ずつ表示してください。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。