2-D arrays: working with a grid · Matrices 2-D: trabajar con una cuadrícula
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].
Una cuadrícula de celdas
- Una matriz 2-D es una cuadrícula: filas y columnas del mismo tipo.
int g[2][3];crea 2 filas y 3 columnas — un total de 6 enteros.- Se accede a una celda con dos í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.
Filas primero, luego columnas (mayor en fila)
- C almacena una matriz fila por fila: todas las filas 0, luego todas las filas 1.
- Para visitar cada celda, use un bucle
forpara las filas, y un bucleforanidado dentro para las columnas. g[r][c]es la celda en la filar, columnac. Ambas empiezan en0.
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.
Pasar una matriz 2-D
- Cuando pasa una matriz a una función, debe dar el número de columnas:
int g[][3]. - C necesita el número de columnas para localizar cada fila en la memoria; el número de filas puede quedar vacío.
- Así, una función recibe
int g[][3], int rows— las columnas están fijas en3, las filas se pasan como parámetro.
#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.
Trabajar fila por fila
- También puede trabajar en una sola fila: recorra solo las columnas para una fila fija
r. - Esta es la forma de sumar una fila o encontrar el valor más grande en una fila.
- Combinar ambos —un bucle externo de filas con un bucle interno de columnas— maneja toda la matriz.
Common mistakes
- Index a grid
grid[row][col]. - The outer loop walks the rows, the inner loop the columns.
Errores comunes
- Indexe una matriz
grid[row][col]. - El bucle externo recorre las filas, el bucle interno recorre las columnas.
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.
Ahora te toca a ti
- Para las tareas de función, la matriz tiene 3 columnas, así que el parámetro es
int g[][3]. - No escriba un
main— el verificador proporcionará uno.
A 2-D array is a grid · Una matriz 2-D es una cuadrícula
Reach a cell with a[row][col] — row first, then column. · Acceder a una celda con a[row][col] — primero la fila, luego la columna.
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 · no write a main. · Completa int sum_grid(int g[][3], int rows) para que devuelva la suma total de todas las celdas en una cuadrícula con rows filas y 3 columnas. No escribas un main.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Complete int row_sum(int g[][3], int r) so it returns the total of the three cells in row r. Do not · no write a main. · Completa int row_sum(int g[][3], int r) para que devuelva la suma de las tres celdas en la fila r. No escribas un main.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
The 2×3 grid g is given. In main, print each row with the three numbers separated by single spaces, one row per line. · La cuadrícula 2×3 g está dada. En main, imprime cada fila con los tres números separados por espacios simples, una fila por línea.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.