Arrays and file handling
引入本课内容
| English | 释义 | 拼音 |
|---|---|---|
| array/əˈreɪ/ | 数组 | shù zǔ |
| file/faɪl/ | 文件 | wén jiàn |
| variable/ˈveərɪəbl/ | 变量 | biàn liàng |
| data/ˈdeɪtə/ | 数据 | shù jù |
| Loops | 循环 | xún huán |
| One-dimensional arrays | 一维数组 | yī wéi shù zǔ |
| 1D array | 一维数组 | yī wéi shù zǔ |
| index/ˈɪndeks/ | 索引 | suǒ yǐn |
| READ | 读取 | dú qǔ |
| Two-dimensional arrays | 二维数组 | èr wéi shù zǔ |
| 2D array/ˌtuː ˈdiː əˈreɪ/ | 二维数组 | èr wéi shù zǔ |
| nested/ˈnestɪd/ | 嵌套 | qiàn tào |
| iteration/ˌɪtəˈreɪʃn/ | 迭代 | dié dài |
| file handling/faɪl ˈhændlɪŋ/ | 文件处理 | wén jiàn chǔ lǐ |
| pseudocode/ˈsuːdəʊkəʊd/ | 伪代码 | wěi dài mǎ |
| loop variable/luːp ˈveərɪəbl/ | 循环变量 | xún huán biàn liàng |
| OPENFILE | 打开文件 | dǎ kāi wén jiàn |
| READFILE | 读取文件 | dú qǔ wén jiàn |
| WRITEFILE | 写入文件 | xiě rù wén jiàn |
| CLOSEFILE | 关闭文件 | guān bì wén jiàn |
数组与文件
- 一个数组在一个变量中存储多个相同类型的值。
- 文件让程序在停止后仍能保留数据。
- 循环是遍历数组和文件的常用方式。
一维数组
- 一维数组就像一个列表;每个值都可以通过索引(位置编号)找到。
DECLARE Scores : ARRAY[1:5] OF INTEGER
Scores[1] ← 90
FOR I ← 1 TO 5
INPUT Scores[I]
NEXT I
- 使用循环可以填充或读取整个数组。

An array is:
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
An array stores many same-type values under one name; each is accessed by its index.
二维数组
- 二维数组就像一个有行和列的表,使用两个索引
[row, column]。
DECLARE Grid : ARRAY[1:3, 1:3] OF INTEGER
Grid[2, 3] ← 8 // row 2, column 3
- 可以用循环中的循环(嵌套迭代)读取二维数组。

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?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The first index is the row, the second the column — row 2, column 3.
Match each idea to what it means.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Arrays hold many values by index; a 2-D array is a grid; nested loops walk it; files are opened, used and closed.
文件处理
- 程序可以将数据存储在文件中,使数据在程序停止后仍然保留。
- 打开文件,使用文件,然后关闭文件:
OPENFILE "data.txt" FOR WRITE
WRITEFILE "data.txt", "Hello"
CLOSEFILE "data.txt"
- 使用完文件后始终要关闭文件(使用
FOR READ重新读取文件)。
Put the steps of using a file in the correct order.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
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.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Closing saves any buffered data and frees the file for other programs.
例题:数组中的最高分
Scores[1:5] 存储着 90 75 60 88 50。编写伪代码找出最高分。
Max ← Scores[1]
FOR Index ← 2 TO 5
IF Scores[Index] > Max
THEN
Max ← Scores[Index]
ENDIF
NEXT Index
OUTPUT Max
Max的初始值是第一个元素,即 90。之后的每个元素都与它比较:75、60、88 和 50 都更小,所以Max保持为 90,输出结果为 90。- 循环变量
Index是索引。在Grid[2, 3]这样的二维数组中,第一个索引表示行,第二个索引表示列。
![分数为 90、75、60、88、50:Max 的初始值为 Scores[1] = 90,之后没有更大的分数,所以 Max 保持为 90](/learn-media/igcse_computer_science/assets/0803-max.png?v=1795984513)
Scores 持有 90 75 60 88 50 with the first index 1. What is Scores[4]?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
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.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Arrays live in RAM while the program runs; a file is saved to storage.
你掌握了
- 一维数组是一个通过索引访问的列表;使用循环填充或读取它
- 二维数组是一个通过
[row, column]访问的表;使用嵌套循环 - 文件处理:OPENFILE(READ/WRITE)→ READFILE/WRITEFILE → CLOSEFILE
- 使用完文件后始终要关闭文件
出了什么问题?只有网站团队能看到这些信息。