1-D arrays · Arreglos 1-D
DECLARE an array
- An array stores many values under one name.
DECLARE Score : ARRAY[1:5] OF INTEGERmakes room for 5 integers.- The numbers
1:5are the first and last index.
DECLARA un array
- Un array almacena muchos valores bajo un solo nombre.
DECLARE Score : ARRAY[1:5] OF INTEGERreserva espacio para 5 enteros.- Los números
1:5son el primer y último índice.
DECLARE Score : ARRAY[1:5] OF INTEGER
Score[1] ← 40
OUTPUT Score[1] // → 40
Arrays count from 1
- The first item is
Score[1], the last isScore[5]. - (This is different from Python and JavaScript, which count from 0.)
Los arrays cuentan desde 1
- El primer elemento es
Score[1], el último esScore[5]. - (Esto es diferente de Python y JavaScript, que cuentan desde 0).
DECLARE Name : ARRAY[1:3] OF STRING
Name[1] ← "Ada"
Name[2] ← "Sam"
OUTPUT Name[2] // → Sam
Loop through an array
- A
FORloop visits each index in turn.
Recorrer un array
- Un bucle
FORvisita cada índice sucesivamente.
DECLARE Score : ARRAY[1:3] OF INTEGER
Score[1] ← 10
Score[2] ← 20
Score[3] ← 30
FOR I ← 1 TO 3
OUTPUT Score[I] // 10, 20, 30
NEXT I
Watch out
- The index must stay inside the declared range:
Score[6]is out of[1:5]. ARRAY[1:5]has 5 cells, numbered 1 to 5 — not 0 to 4.
Ten cuidado
- El índice debe permanecer dentro del rango declarado:
Score[6]está fuera de[1:5]. ARRAY[1:5]tiene 5 celdas, numeradas del 1 al 5 — no del 0 al 4.
Common mistakes
- Declare an array with its bounds:
DECLARE A : ARRAY[1:N] OF INTEGER. - Index only within the declared range.
Errores comunes
- Declara un array con sus límites:
DECLARE A : ARRAY[1:N] OF INTEGER. - Accede al índice únicamente dentro del rango declarado.
Now you try
- Declare an array, fill it, then read or loop over it.
- Press Check answer to test it.
Tú puedes
- Declara un array, llénalo, luego léelo o recórrerlo.
- Pulsa Comprobar respuesta para ponerlo a prueba.
Declare an array A of 3 integers. Store 10, 20, 30 in positions 1, 2, 3, then output A[2] (the answer is 20). · Declarar un arreglo A de 3 enteros. Almacenar 10, 20, 30 en las posiciones 1, 2, 3, luego imprimir A[2] (la respuesta es 20).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
The array A holds · contiene 5, 8, 2. Use a FOR loop to add them and output the total (it is 15). · El arreglo A contiene 5, 8, 2. Usar un bucle FOR para sumarlos e imprimir el total (es 15).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
The array A holds · contiene 3, 9, 2, 7. Find the largest value and output it (it is 9). · El arreglo A contiene 3, 9, 2, 7. Encontrar el valor más grande e imprimirlo (es 9).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.