2-D arrays · Arreglos 2-D
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · Esta página necesita un navegador reciente (con soporte para SharedArrayBuffer). Por favor, actualiza Chrome, Edge, Firefox o Safari a la última versión.
English
Rows and columns
- A two-dimensional array is a grid. The declaration gives both bounds.
- Two indexes, separated by a comma, pick a cell: row, then column.
Español
Filas y columnas
- Una matriz bidimensional es una cuadrícula. La declaración proporciona ambos límites.
- Dos índices, separados por una coma, seleccionan una celda: fila, luego columna.
English
Español
DECLARE Grid : ARRAY[1:2, 1:2] OF INTEGER
Grid[1, 1] ← 4
Grid[1, 2] ← 7
OUTPUT Grid[1, 2]
English
One loop inside another
- The outer loop walks the rows. The inner loop walks the columns of that row.
NEXTnames the counter of the loop it closes.
Español
Un bucle dentro de otro
- El bucle exterior recorre las filas. El bucle interior recorre las columnas de esa fila.
NEXTnombra al contador del bucle que cierra.
DECLARE Grid : ARRAY[1:2, 1:2] OF INTEGER
Grid[1, 1] ← 1
Grid[1, 2] ← 2
Grid[2, 1] ← 3
Grid[2, 2] ← 4
DECLARE Total : INTEGER
Total ← 0
FOR Row ← 1 TO 2
FOR Column ← 1 TO 2
Total ← Total + Grid[Row, Column]
NEXT Column
NEXT Row
OUTPUT Total
English
Common mistakes
- Write
Grid[1, 2], with a comma. Both indexes start at the declared lower bound. - The inner
NEXTcomes before the outer one.
Español
Errores comunes
- Escribir
Grid[1, 2], con una coma. Ambos índices comienzan en el límite inferior declarado. - El
NEXTinterno viene antes que el externo.
English
Now you try
- Read one cell of a 2-D array, then total the grid.
Español
Ahora te toca a ti
- Leer una celda de una matriz 2-D, luego sumar todos los valores de la matriz.
Declare Grid as ARRAY[1:2, 1:2] OF INTEGER. Store 7 in row 1, column 2, and output that cell. · Declara Grid como ARRAY[1:2, 1:2] OF INTEGER. Almacena 7 en la fila 1, columna 2, y muestra esa celda.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
The grid is filled for you. Total all four cells and output the total (10). · La cuadrícula está llena por ti. Suma las cuatro celdas y muestra el total (10).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.