1-D arrays
Cambridge Pseudocode Lesson 10 1:59 English narration · English + 中文 subtitles burned in
Chapters
Transcript
An array stores many values under one name, and the declaration says exactly three things.
数组用一个名字存放很多个值,而声明恰好说明三件事。
Score is one name for all of them.
Score 是它们全体共用的一个名字。
The two numbers in brackets are the first and the last index — not a size, an index pair, and that distinction matters in a moment.
方括号里的两个数字是首下标和末下标—— 不是"大小",而是一对下标, 这个区别马上就有用。
And OF INTEGER says what each cell holds.
而 OF INTEGER 说明每个格子里装什么。
Here are those five cells with both numberings on them, exactly as lesson nine did for characters.
这是那五个格子,上面标着两套编号, 和第 9 课对字符做的完全一样。
The row above is the index the exam uses, starting at one.
上面那一排是考卷用的下标,从 1 开始。
The row below is what Python would call the same cell, starting at nought.
下面那一排是 Python 对同一个格子的叫法,从 0 开始。
So read the declaration again: ARRAY one colon five gives you five cells, numbered one to five — not nought to four.
所以再读一遍那个声明: ARRAY 给你五个格子,编号 1 到 5——不是 0 到 4。
Using one cell needs nothing new.
使用一个格子不需要学任何新东西。
Put the index in brackets after the name and the whole thing behaves exactly like a variable: you can store into it with the arrow, and you can read it in an OUTPUT.
在名字后面的方括号里写上下标, 整个东西的行为就和一个变量完全一样: 你可以用箭头往里存,也可以在 OUTPUT 里读它。
Name two is Sam, so that is what prints.
Name 是 "Sam",所以打印出来的就是它。
And here is the combination worth practising until it is automatic.
而这就是值得练到成为条件反射的那个组合。
Put a FOR loop's counter inside the brackets, and i names the cell the loop is on.
把一个 FOR 循环的计数器放进方括号里, i 就指出了循环当前所在的那个格子。
One pass, one cell, in order.
一趟一个格子,按顺序来。
An array plus a FOR loop is behind half the paper — totalling, counting, searching, finding the largest — so get this shape into your hands now.
数组加 FOR 循环撑起了半张卷子—— 求和、计数、查找、找最大值—— 所以现在就把这个形状练进手里。
Four things to take with you.
带走四点。
One: DECLARE with ARRAY one colon five gives five cells.
第一:用 ARRAY 声明会得到五个格子。
Two: those numbers are the first and last index, not a size.
第二:那两个数字是首下标和末下标,不是大小。
Three: reach one cell with the name and an index.
第三:用名字加一个下标去访问某一个格子。
Four: stay inside the range, because Score six is not there.
第四:待在范围之内,因为 Score 根本不存在。
Now do the three tasks.
现在去做那三道题。