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를 만듭니다.- 두 개의 인덱스로 하나의 셀에 접근합니다:
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.
행 이후 열 (행 우선 순서)
- 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.
한 행씩 작업하기
- 단일 행에 대해 작업할 수도 있습니다: 고정된 행
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]**을 가진 셀에 도달하십시오 — 먼저 행(row), ensuite 열(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열로 이루어진 격자/grid의 모든 셀의 합을 반환하도록 하십시오. **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의 세 셀 값의 합을 반환하도록 하십시오. **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. · 2×3 크기의 격자 g이 주어졌습니다. main에서 각 행을 출력하되, 숫자 사이에는 공백 하나를 넣고 한 줄에 하나의 행만 출력하십시오.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.