Arrays and file handling
Introduzido
13:08
Programação
Aqui está um programa curto. Ele lê cinco notas de teste e imprime o total. As cinco notas são oito, nove, sete, seis e dez, então o total deve ser quarenta. Observe…
Narração em inglês · legendas em inglês e 中文 que você pode alternar
Nesta aula
| English | Significado |
|---|---|
| array/əˈreɪ/ | |
| 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/ | aninhado |
| iteration/ˌɪtəˈreɪʃn/ | Repeating a sequence of instructions or calculations. |
| file handling/faɪl ˈhændlɪŋ/ | manipulação de arquivos |
| 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 |
Array 数组 e ficheiros
- Um array armazena muitos valores do mesmo tipo numa única variável.
- Um ficheiro permite a um programa guardar dados depois de parar.
- Los loops são a forma natural de percorrer ambos.
Arrays unidimensionais
- Um array 1D é como uma lista simples; cada valor é encontrado por um índice 索引 (um número de posição).
DECLARE Scores : ARRAY[1:5] OF INTEGER
Scores[1] ← 90
FOR I ← 1 TO 5
INPUT Scores[I]
NEXT I
- Um loop preenche ou lê todo o array.

Um array 1D guarda muitos valores numa variável; cada um é encontrado pelo seu índice.
An array is:
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
An array stores many same-type values under one name; each is accessed by its index.
Arrays bidimensionais
- Um array 2D é como uma tabela com linhas e colunas, usando dois índices
[row, column].
DECLARE Grid : ARRAY[1:3, 1:3] OF INTEGER
Grid[2, 3] ← 8 // row 2, column 3
- Lê-se um array 2D com um loop dentro de um loop (iteração aninhada 嵌套).

Um array 2D é uma tabela; cada valor é encontrado por dois índices, (linha, coluna).
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?
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
The first index is the row, the second the column — row 2, column 3.
Match each idea to what it means.
Dificuldade 2/5 · Moedas 140 + pequeno bônus na primeira conclusão hoje
Arrays hold many values by index; a 2-D array is a grid; nested loops walk it; files are opened, used and closed.
Gestão de ficheiros 文件处理
- Um programa pode guardar dados num ficheiro para sobreviver após o programa parar.
- Abrir o ficheiro, usá-lo, depois fechar-o:
OPENFILE "data.txt" FOR WRITE
WRITEFILE "data.txt", "Hello"
CLOSEFILE "data.txt"
- Sempre file um ficheiro quando terminar (use
FOR READpara o ler novamente).

Um contador conta até 1; um total constrói uma soma acumulada
Put the steps of using a file in the correct order.
Dificuldade 3/5 · Moedas 200 + pequeno bônus na primeira conclusão hoje
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.
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
Closing saves any buffered data and frees the file for other programs.
Este cartão é novo, por isso aparece em inglês até que a tradução esteja pronta.
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 contém 90 75 60 88 50 with the first index 1. What is Scores[4]?
Dificuldade 2/5 · Moedas 140 + pequeno bônus na primeira conclusão hoje
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.
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
Arrays live in RAM while the program runs; a file is saved to storage.
Entendeu?
- um array 1D é uma lista acessada por um índice; preencher/lê-lo com um loop
- um array 2D é uma tabela acessada por
[row, column]; use loops aninhados - gestão de ficheiros: OPENFILE (READ/WRITE) → READFILE/WRITEFILE → CLOSEFILE
- sempre file um ficheiro quando terminar
O que deu errado? Somente a equipe do site vê isso.