Skip to content

2-D arrays

Cambridge Pseudocode Lesson 11 2:08 English narration · English + 中文 subtitles burned in

space play · ←/→ 5s · j/l 10s · f fullscreen · ,/. speed

Chapters

Transcript
A 2-D array is a grid — a table with rows and columns. 二维数组是一个网格——一张有行有列的表。
The declaration has two index pairs instead of one, and each pair does the same job it did in lesson ten. 它的声明里有两对下标,而不是一对, 每一对做的都是第 10 课里同样的事。
The first pair says two rows. 第一对说:两行。
The second says three columns. 第二对说:三列。
So this array holds six values, and every one of them has a row number and a column number. 所以这个数组装了六个值, 而其中每一个都有一个行号和一个列号。
To reach one cell you give both numbers, and the order is fixed: row first, then column. 要访问一个格子,你要给出两个数字,而顺序是固定的:先行,后列。
G one two means row one, column two, which is the eight. G 表示第 1 行、第 2 列,也就是那个 8。
Now swap them. 现在把它们对调。
G two one means row two, column one, which is the seven — a different cell, and a different value. G 表示第 2 行、第 1 列,也就是那个 7—— 另一个格子,另一个值。
The important part: that is not an error. 重点在于:这不是一个错误。
Nothing warns you; you simply get the wrong number. 没有任何东西会警告你;你只是拿到了错的数字。
To visit every cell you need two loops, one inside the other. 要访问每一个格子,你需要两层循环,一层套在另一层里面。
Watch the two counters. 看这两个计数器。
For each single value of r, the inner loop runs all the way through c — one, two, three — and only then does r move on. 对于 r 的每一个取值,内层循环都会把 c 从头跑到尾—— 1、2、3——之后 r 才往前走一步。
That is why the grid is covered row by row rather than column by column, and it is what a trace-table question is checking. 这就是为什么这个网格是一行一行地被覆盖的,而不是一列一列, 也是一道追踪表题目在检查的东西。
One more order to get right, and it is the second-most-common mistake here. 还有一个顺序要弄对,而它是这里第二常见的错误。
The inner loop opened last, so it must close first: NEXT c, then NEXT r. 内层循环是最后打开的,所以它必须最先关闭: 先 NEXT c,再 NEXT r。
Write them the other way round and the two loops cross over, which is not a program at all. 反过来写,两个循环就交叉了,那根本不是一个程序。
Loops nest like brackets — the one opened last is the one closed first. 循环像括号一样嵌套——最后打开的那个,就是最先关闭的那个。
Four things to take with you. 带走四点。
One: ARRAY with two pairs is r rows by c columns. 第一:带两对下标的 ARRAY 是 r 行 c 列。
Two: an index pair is row first, then column. 第二:一对下标是先行、后列。
Three: the outer loop walks the rows and the inner one columns. 第三:外层循环走行,内层循环走列。
Four: the inner NEXT closes before the outer one. 第四:内层的 NEXT 在外层之前关闭。
Now do the three tasks. 现在去做那三道题。

Log in or create account

IGCSE, A-Level & AP