2-D arrays: working with a grid · Arrays 2-D: trabalhando com uma grade
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].
Uma grade de caixas
- Uma matriz 2D é uma grade: linhas e colunas do mesmo tipo.
int g[2][3];cria 2 linhas e 3 colunas — 6 ints no total.- Você acessa uma célula com dois índices:
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.
Linhas primeiro, depois colunas (row-major)
- C armazena uma grade linha por linha: toda a linha 0, depois toda a linha 1.
- Para visitar todas as células, use um loop
forpara as linhas, e um loopfordentro dele para as colunas. g[r][c]é a célula na linhar, colunac. Ambas começam em0.
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.
Passando uma matriz 2D
- Ao passar uma grade para uma função, você deve dar o número de colunas:
int g[][3]. - C precisa do número de colunas para encontrar cada linha na memória; o número de linhas pode ficar vazio.
- Então a função recebe
int g[][3], int rows— as colunas são fixas em3, as linhas são passadas.
#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.
Trabalhando uma linha de cada vez
- Você também pode trabalhar em uma única linha: faça loop apenas nas colunas para uma linha fixa
r. - É assim que você soma uma linha ou encontra o maior valor em uma linha.
- Misturar os dois — um loop externo de linhas com um interno de colunas — lida com a grade inteira.
Common mistakes
- Index a grid
grid[row][col]. - The outer loop walks the rows, the inner loop the columns.
Erros comuns
- Indexe uma grade
grid[row][col]. - O loop externo percorre as linhas, o interno as colunas.
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.
Agora você tenta
- Para as tarefas de função, a grade tem 3 colunas, então o parâmetro é
int g[][3]. - Não escreva um
main— o verificador fornece um.
A 2-D array is a grid · Um array 2-D é uma grade
Reach a cell with a[row][col] — row first, then column. · Acesse uma célula com a[row][col] — linha primeiro, depois coluna.
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 · não write a main. · Complete int sum_grid(int g[][3], int rows) para que retorne o total de todas as células em uma grade com rows linhas e 3 colunas. Não escreva um main.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Complete int row_sum(int g[][3], int r) so it returns the total of the three cells in row r. Do not · não write a main. · Complete int row_sum(int g[][3], int r) para que retorne o total das três células na linha r. Não escreva um main.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
The 2×3 grid g is given. In main, print each row with the three numbers separated by single spaces, one row per line. · A grade 2×3 g é dada. Em main, imprima cada linha com os três números separados por espaços simples, uma linha por linha.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.