1-D arrays · 一维数组
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
A list of the same type
- An array holds a fixed number of values of one type.
- The guide's lower bound is 1 unless a question says otherwise.
- One index, in square brackets, picks an element.
中文
同一类型的一列值
- 数组保存固定个数、同一类型的值。
- 除非题目另有说明,指南的下标下界是 1。
- 方括号里的一个下标选出一个元素。
English
中文
DECLARE Score : ARRAY[1:5] OF INTEGER
Score[1] ← 4
Score[2] ← 8
OUTPUT Score[1]
English
A loop visits every cell
- A
FORloop from the lower bound to the upper bound is how you fill or read the array.
中文
用循环访问每一个格子
- 从下界到上界的
FOR循环用来填入或读出数组。
DECLARE Score : ARRAY[1:3] OF INTEGER
Score[1] ← 4
Score[2] ← 8
Score[3] ← 6
DECLARE Total : INTEGER
Total ← 0
FOR I ← 1 TO 3
Total ← Total + Score[I]
NEXT I
OUTPUT Total
English
Watch out
Score[1]is the first cell when the array was declared[1:5].- The index can be an expression:
Score[I + 1].
中文
注意
- 数组声明为
[1:5]时,Score[1]是第一个格子。 - 下标可以是表达式:
Score[I + 1]。
English
Common mistakes
- Declare the array before you store into a cell.
- The index must stay inside the declared bounds.
中文
常见错误
- 先声明数组,再往格子里存值。
- 下标必须落在声明的范围内。
English
Now you try
- Store three numbers and total them.
中文
现在你来试
- 存入三个数,再把它们相加。
Declare Score as ARRAY[1:3] OF INTEGER. Store 4 in cell 1 and output that cell. · 把 Score 声明为 ARRAY[1:3] OF INTEGER。在第 1 格存入 4,并输出这一格。
Click Run to see the output here. · 点击“运行”查看此处输出。
The array is filled for you. Total the three values and output the total (18). · 数组已为你填好。把三个值相加,并输出总和(18)。
Click Run to see the output here. · 点击“运行”查看此处输出。