2-D arrays · Arrays 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 precisa de um navegador recente (com suporte a SharedArrayBuffer). Por favor, atualize Chrome, Edge, Firefox ou Safari para a versão mais recente.
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.
Português
Linhas e colunas
- Um array bidimensional é uma grade. A declaração fornece ambos os limites.
- Dois índices, separados por vírgula, selecionam uma célula: linha, depois coluna.
English
Português
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.
Português
Um loop dentro do outro
- O loop externo percorre as linhas. O loop interno percorre as colunas dessa linha.
NEXTnomeia o contador do loop que ele fecha.
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.
Português
Erros comuns
- Escreva
Grid[1, 2], separado por vírgula. Ambos os índices começam no limite inferior declarado. - O
NEXTinterno vem antes do externo.
English
Now you try
- Read one cell of a 2-D array, then total the grid.
Português
Agora você tenta
- Leia uma célula de uma matriz 2D e, em seguida, some a grade inteira.
Declare Grid as ARRAY[1:2, 1:2] OF INTEGER. Store 7 in row 1, column 2, and output that cell. · Declare Grid como ARRAY[1:2, 1:2] OF INTEGER. Armazene 7 na linha 1, coluna 2, e imprima essa célula.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
The grid is filled for you. Total all four cells and output the total (10). · A grade é preenchida para você. Some todas as quatro células e imprima o total (10).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.