Arrays and file handling
Introducido
13:08
Programación
Aquí hay un programa corto. Lee cinco notas de prueba e imprime el total. Las cinco notas son ocho, nueve, siete, seis y diez, así que el total debería ser cuarenta. Mira…
Narración en inglés · subtítulos en inglés y 中文 que puedes cambiar
En esta lección
| English | Significado |
|---|---|
| array/əˈreɪ/ | matriz |
| file/faɪl/ | A named collection of data stored and managed as a unit. |
| variable/ˈveərɪəbl/ | A quantity or named value that can change or take different values. |
| data/ˈdeɪtə/ | Recorded facts, observations or values used for analysis or processing. |
| Loops | The natural way to work through both. |
| One-dimensional arrays | |
| 1D array | |
| index/ˈɪndeks/ | índice |
| READ | |
| Two-dimensional arrays | |
| 2D array/ˌtuː ˈdiː əˈreɪ/ | |
| nested/ˈnestɪd/ | anidado |
| iteration/ˌɪtəˈreɪʃn/ | Repeating a sequence of instructions or calculations. |
| file handling/faɪl ˈhændlɪŋ/ | gestión de archivos |
| pseudocode/ˈsuːdəʊkəʊd/ | A language-independent description of an algorithm using structured, programming-like statements. |
| loop variable/luːp ˈveərɪəbl/ | |
| OPENFILE | |
| READFILE | |
| WRITEFILE | |
| CLOSEFILE |
Arrays 数组 y archivos
- Una array (matriz) almacena muchos valores del mismo tipo en una sola variable.
- Un archivo permite a un programa conservar datos después de que se detiene.
- Los bucles son la forma natural de recorrer ambos.
Arrays unidimensionales
- Una array 1D es como una lista simple; cada valor se encuentra mediante un índice (un número de posición).
DECLARE Scores : ARRAY[1:5] OF INTEGER
Scores[1] ← 90
FOR I ← 1 TO 5
INPUT Scores[I]
NEXT I
- Un bucle rellena o lee toda la array.

Una array 1D contiene muchos valores en una variable; cada uno se encuentra por su índice.
An array is:
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
An array stores many same-type values under one name; each is accessed by its index.
Arrays bidimensionales
- Una array 2D es como una tabla con filas y columnas, utilizando dos índices
[row, column].
DECLARE Grid : ARRAY[1:3, 1:3] OF INTEGER
Grid[2, 3] ← 8 // row 2, column 3
- Se lee una array 2D con un bucle dentro de otro bucle (iteración anidada).

Una array 2D es una tabla; cada valor se encuentra mediante dos índices, (fila, columna).
Index a 2-D array: Grid[Row, Col]
A 2-D array is a grid of values. Grid[Row, Col] reaches exactly one cell — the row first, then the column. Cambridge counts both from 1.
In grid[2, 3], which cell is accessed?
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
The first index is the row, the second the column — row 2, column 3.
Match each idea to what it means.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
Arrays hold many values by index; a 2-D array is a grid; nested loops walk it; files are opened, used and closed.
Manejo de archivos
- Un programa puede almacenar datos en un archivo para que sobrevivan después de que el programa se detenga.
- Abrir el archivo, usarlo y luego cerrarlo:
OPENFILE "data.txt" FOR WRITE
WRITEFILE "data.txt", "Hello"
CLOSEFILE "data.txt"
- Siempre cierra un archivo cuando termines (usa
FOR READpara leerlo de nuevo).

Un contador suma de 1 en 1; un total construye una suma acumulativa
Put the steps of using a file in the correct order.
Dificultad 3/5 · 200 monedas + un pequeño bono en el primer intento de hoy
Always open first, then do the reading/writing, then close to save buffered data and free the file.
You should always close a file when you have finished using it.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Closing saves any buffered data and frees the file for other programs.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
Worked example: the highest score in an array
Scores[1:5] holds 90 75 60 88 50. Write pseudocode 伪代码 to find the highest score.
Max ← Scores[1]
FOR Index ← 2 TO 5
IF Scores[Index] > Max
THEN
Max ← Scores[Index]
ENDIF
NEXT Index
OUTPUT Max
Maxstarts as the first element, 90. Each later element is compared with it: 75, 60, 88 and 50 are all smaller, soMaxstays 90 and 90 is output.- The loop variable 循环变量
Indexis the index. In a 2D array such asGrid[2, 3], the first index is the row and the second is the column.
![Scores 90, 75, 60, 88, 50: Max starts at Scores[1] = 90 and no later score is bigger, so Max stays 90](/learn-media/igcse_computer_science/assets/0803-max.png?v=1795984513)
Scores contiene 90 75 60 88 50 with the first index 1. What is Scores[4]?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
Counting from index 1: 90, 75, 60, 88.
Data in an array is lost when the program stops, but data written to a file is kept.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Arrays live in RAM while the program runs; a file is saved to storage.
Ya lo dominas
- una array 1D es una lista accedida por un solo índice; rellénala/léela con un bucle
- una array 2D es una tabla accedida por
[row, column]; usa bucles anidados - manejo de archivos: OPENFILE (READ/WRITE) → READFILE/WRITEFILE → CLOSEFILE
- siempre cierra un archivo cuando hayas terminado
¿Qué salió mal? Solo el equipo del sitio ve esta información.