8.2 · Arrays
8.2 · 数组
Arrays: declaring, using, and looping through them
数组:声明、使用与循环遍历
Three skills the same 15-mark Paper 2 question keeps testing: choosing an array in the first place, declaring it so it would actually run, and reaching every element with a loop.
同一道 15 分的试卷二题目反复考察三项技能:首先选择使用数组,把它声明得能够真正运行,以及用循环访问每一个元素。
Explainer · 8.2.2
讲解 · 8.2.2
Why use an array at all
为什么要使用数组
Isla wants to store one exam mark for each of 30 students. Without an array she needs 30 separate variables: Mark1, Mark2, all the way to Mark30. To add them up she writes 30 lines. To change the class size she rewrites the lot. It works, but nothing about it scales.
Isla 想为 30 名学生各存储一个考试成绩。如果不用数组,她需要 30 个独立的变量:Mark1、Mark2,一直到 Mark30。要把它们加起来,她得写 30 行代码。要改变班级人数,她得把这些全部重写。这样做行得通,但完全无法扩展。
An array replaces all 30 with one named structure. Every value sits in a numbered slot, and one loop can visit every slot in turn.
一个数组用一个命名的结构取代了全部 30 个变量。每一个值都存放在一个编号的槽位中,一个循环就能依次访问每一个槽位。
A one-dimensional array is a list of items of the same data type, stored under a single identifier, where each item is reached by its index (its position in the list): StudentMark[1], StudentMark[2], and so on.
一维数组是一列相同数据类型的项目,存储在单一标识符之下,每个项目通过其索引(它在列表中的位置)来访问:StudentMark[1]、StudentMark[2],依此类推。
That is the description examiners want. When a question asks why an array is a good choice, the marks are for the benefit, not the description: one identifier for many related values, and an index you can move through with a loop to reach, total, search or sort every element without repeating code.
这正是考官想要的描述。但当题目问的是为什么数组是个好选择时,得分点在于说明其益处,而不是描述定义:一个标识符对应多个相关的值,并且可以用循环遍历索引,从而访问、求和、查找或排序每一个元素,而无需重复代码。
Before writing code, ask: do I have many values of the same kind that I would otherwise number by hand (Score1, Score2, ...)? If yes, that is an array. If one value also depends on a second thing (a student and a subject), that is a 2D array, covered further down.
写代码之前先问自己:我是否有许多同类型的值,否则就得手动编号(Score1、Score2……)?如果是,那就该用数组。如果一个值还同时取决于第二个因素(既有学生又有科目),那就是二维数组,详见下文。
Explainer · 8.2.1
讲解 · 8.2.1
Declaring a 1D array
声明一维数组
A declaration tells the program the array's name, how many elements it holds, and what type of value goes in each one.
声明语句告诉程序数组的名称、它包含多少个元素,以及每个元素存放什么类型的值。
DECLARE StudentMark : ARRAY[1:30] OF INTEGER creates an array called StudentMark with 30 elements, numbered 1 to 30, each holding a whole number. The shape is always ARRAY[lower:upper] OF <type>: only the name, the two bounds and the type change.
DECLARE StudentMark : ARRAY[1:30] OF INTEGER 创建了一个名为 StudentMark、包含 30 个元素的数组,编号从 1 到 30,每个元素存放一个整数。这个格式始终是 ARRAY[下界:上界] OF <类型>:只有名称、两个边界和类型会变化。
The lower bound can legally be 0 or 1, it depends entirely on the DECLARE statement, there is no universal rule (though most real languages number from 0 by default). ARRAY[1:30] runs 1 to 30; ARRAY[0:29] also gives 30 elements but numbered from 0. Read the declared bounds before you assume where the count starts, and use those same numbers in every loop that touches the array.
下界可以合法地是 0 或 1,这完全取决于 DECLARE 语句本身,并没有统一规则(尽管大多数真实语言默认从 0 开始编号)。ARRAY[1:30] 编号 1 到 30;ARRAY[0:29] 同样是 30 个元素,但从 0 开始编号。在假设计数从哪里开始之前,先看清声明的边界,并在每个涉及该数组的循环中使用相同的数字。
The identifier has to be usable in a real program, and it has to be meaningful. Examiners repeatedly note marks lost for a name with a space or punctuation in it (Student Mark), for a bland name like Array that says nothing about what it holds, or for switching to a different name partway through an answer. Pick one descriptive single-word name, use it unchanged everywhere, and use the exact variable names a question gives you rather than inventing your own.
标识符必须能在真实程序中使用,而且必须具有意义。考官多次指出,考生因名称中带有空格或标点(Student Mark)、使用像 Array 这样毫无意义的平淡名称,或在同一份答案中途换成不同的名称而失分。请选择一个具有描述性的单词作为名称,全程保持不变,并使用题目给出的确切变量名,而不是自创名称。
To reach one element you put an expression in the brackets. A fixed number reads one slot: StudentMark[1]. A variable as the index is what makes a loop work: StudentMark[Count] reads whichever slot Count currently points at, so a single line inside a loop can touch all 30.
要访问某一个元素,需要在方括号中放入一个表达式。固定的数字读取单个槽位:StudentMark[1]。而用变量作为索引才是循环得以运作的关键:StudentMark[Count] 读取的是 Count 当前所指向的槽位,因此循环内的一行代码就能访问全部 30 个元素。
Worked example · 8.2.1 + 8.2.3
示例讲解 · 8.2.1 + 8.2.3
Fill a 1D array, then read it back
填充一维数组,再读取它
Isla records her step count for each of the 7 days in a week. One array holds all seven values; one loop fills it from input, a second loop adds every element to a total.
Isla 记录了一周 7 天每天的步数。一个数组存放全部七个数值;一个循环从输入中填充它,第二个循环把每个元素加到一个总数中。
Click a slot to see how it’s indexed.
点击一个槽位,查看它是如何被索引的。
| index | 索引 | [1] | [2] | [3] | [4] | [5] | [6] | [7] |
|---|---|---|---|---|---|---|---|---|
| StepCount | StepCount[3] |
The index is the position in the list, nothing more. StepCount[3] holds the value 7650 (day 3). Change the number in the brackets and you move along the row.
DECLARE StepCount : ARRAY[1:7] OF INTEGER DECLARE Day : INTEGER DECLARE TotalSteps : INTEGER // Fill the array: one input per day FOR Day ← 1 TO 7 OUTPUT "Steps for day ", Day, ": " INPUT StepCount[Day] NEXT Day // Read the array back: add every element to a total TotalSteps ← 0 FOR Day ← 1 TO 7 TotalSteps ← TotalSteps + StepCount[Day] NEXT Day OUTPUT "Total steps this week: ", TotalSteps
# index 0 is unused so days line up 1 to 7 StepCount = [0] * 8 for day in range(1, 8): StepCount[day] = int(input("Steps for day " + str(day) + ": ")) total_steps = 0 for day in range(1, 8): total_steps += StepCount[day] print("Total steps this week:", total_steps)
int[] stepCount = new int[8]; // indexes 1..7 used Scanner sc = new Scanner(System.in); for (int day = 1; day <= 7; day++) { System.out.print("Steps for day " + day + ": "); stepCount[day] = sc.nextInt(); } int totalSteps = 0; for (int day = 1; day <= 7; day++) { totalSteps += stepCount[day]; } System.out.println("Total steps this week: " + totalSteps);
Dim StepCount(7) As Integer Dim day As Integer For day = 1 To 7 Console.Write("Steps for day " & day & ": ") StepCount(day) = Console.ReadLine() Next day Dim totalSteps As Integer = 0 For day = 1 To 7 totalSteps = totalSteps + StepCount(day) Next day Console.WriteLine("Total steps this week: " & totalSteps)
Both loops run 1 TO 7, the same bounds as the declaration. The index Day is a variable, so the one line INPUT StepCount[Day] fills a different slot on every pass.
两个循环都运行 1 TO 7,与声明的边界一致。索引 Day 是一个变量,因此 INPUT StepCount[Day] 这一行代码每次执行时都会填充不同的槽位。
Initialising an array is the same loop shape. A frequent exam task is setting every element to a starting value before use. Examiners note candidates can write the loop but stumble on filling every slot. It is just: FOR Index ← 1 TO 20 / Totals[Index] ← 0 / NEXT Index, one assignment repeated for each element.
初始化数组用的是同样的循环结构。一项常见的考试任务是在使用前将每个元素设为初始值。考官指出,考生能写出循环,却在填充每一个槽位时出错。其实很简单:FOR Index ← 1 TO 20 / Totals[Index] ← 0 / NEXT Index,对每个元素重复同一条赋值语句。
Try it yourself
动手试试
Run the 1D array code
运行一维数组代码
The 1D example from just above, live and editable. Python is one of the four languages Cambridge accepts for the Paper 2 scenario question. One difference from the pseudocode: Python counts indexes from 0, so day 1 sits at StepCount[0]. Two experiments are waiting at the bottom.
这是上面一维数组示例的实时可编辑版本。Python 是剑桥试卷二情境题接受的四种语言之一。与伪代码的一个区别:Python 的索引从 0 开始计数,因此第 1 天存放在 StepCount[0]。底部还准备了两个实验供你尝试。
Running in offline fallback mode. The full Python engine couldn’t load (blocked school network, or no internet), so this console switched automatically to a small hand-written engine. It runs lists, indexing, range(), for loops, len() and nested lists, which covers every array example on this page, but it is not full Python. One visible difference: a list of text prints without quotes ([a, b] rather than ['a', 'b']).
正在以离线备用模式运行。完整的 Python 引擎未能加载(学校网络受限,或没有网络连接),因此本控制台自动切换到了一个小型的手写引擎。它支持列表、索引、range()、for 循环、len() 以及嵌套列表,足以覆盖本页的每一个数组示例,但它并非完整的 Python。一个可见的差异是:文本列表打印时不带引号(显示为 [a, b] 而非 ['a', 'b'])。
Check your understanding
检验你的理解
Isla needs one array to hold a rainfall reading (a decimal) for each of 15 days. Which declaration is correct?
Isla 需要一个数组来存储 15 天中每天的降雨量读数(小数)。哪一个声明是正确的?
Explainer · 8.2.1 + 8.2.3
讲解 · 8.2.1 + 8.2.3
2D arrays and nested iteration
二维数组与嵌套迭代
Think of a class register: one row of numbered seats, one value per seat. Now think of a spreadsheet: rows AND columns, and every cell needs two coordinates to find it, not one. That jump, from one number to two, is the entire difference between a 1D array and a 2D array.
想象一份班级名册:一排编号的座位,每个座位一个值。再想象一份电子表格:既有行又有列,每个单元格需要两个坐标才能定位,而不是一个。从一个数字到两个数字的这一跳跃,正是一维数组与二维数组之间的全部区别。
A 2D array is a grid, and every box needs two indices to find it: one for the row, one for the column.
一个二维数组是一个网格,每个格子都需要两个索引才能定位:一个是行,一个是列。
The syllabus declares a 2D array as rows first, then columns: DECLARE WeeklyUsage : ARRAY[1:50, 1:5] OF INTEGER means 50 rows (spaces) each with 5 columns (days). Reading it is WeeklyUsage[Space, Day], always in that declared order.
考纲规定二维数组的声明先行后列:DECLARE WeeklyUsage : ARRAY[1:50, 1:5] OF INTEGER 表示 50 行(车位),每行 5 列(天数)。读取时写作 WeeklyUsage[Space, Day],顺序始终与声明一致。
Nested iteration is how you visit every box in a 2D array: an outer loop walks the rows, an inner loop walks the columns of the current row. The inner loop finishes completely before the outer loop moves to the next row.
嵌套迭代是访问二维数组每个格子的方式:外层循环遍历行,内层循环遍历当前行的列。内层循环必须完全结束,外层循环才会移动到下一行。
Paper 2 scenarios lean on this shape every year: a grid of readings, one row per thing, one column per time slot (screen time per student per day, temperatures per station per week, hours per parking space per day). Here it is 50 spaces, 5 days, hours recorded in WeeklyUsage[Space, Day]. To total one space, the inner loop must finish all 5 days before the outer loop moves on.
试卷二的情境题年年都依赖这种结构:一个数据网格,每行对应一个对象,每列对应一个时间段(每个学生每天的屏幕使用时长、每个站点每周的气温、每个车位每天的时长)。这里是 50 个车位、5 天,时长记录在 WeeklyUsage[Space, Day] 中。要统计一个车位的总时长,内层循环必须先完成全部 5 天,外层循环才能继续。
Click a cell to see how it’s indexed.
点击一个单元格,查看它是如何被索引的。
| WeeklyUsage | Day 1 | 第 1 天 | Day 2 | 第 2 天 | Day 3 | 第 3 天 | Day 4 | 第 4 天 | Day 5 | 第 5 天 |
|---|---|---|---|---|---|---|---|---|---|---|
| Space 11 | 车位 11 | |||||||||
| Space 12 | 车位 12 | WeeklyUsage[12,3] | ||||||||
| Space 13 | 车位 13 |
Row = space (outer index), column = day (inner index), matching declaration order. WeeklyUsage[12,3] is Space 12’s Day 3 value, not Day 12 of Space 3. The hours recorded is 6.
// Total hours parked for each of the 50 spaces, across all 5 days DECLARE Space : INTEGER DECLARE Day : INTEGER DECLARE TotalHours : INTEGER FOR Space ← 1 TO 50 TotalHours ← 0 // reset BEFORE the inner loop starts FOR Day ← 1 TO 5 TotalHours ← TotalHours + WeeklyUsage[Space, Day] NEXT Day OUTPUT "Space ", Space, " total hours: ", TotalHours NEXT Space
# Total hours parked for each of the 50 spaces, across all 5 days for space in range(1, 51): total_hours = 0 for day in range(1, 6): total_hours += WeeklyUsage[space][day] print("Space", space, "total hours:", total_hours)
// Total hours parked for each of the 50 spaces, across all 5 days for (int space = 1; space <= 50; space++) { int totalHours = 0; for (int day = 1; day <= 5; day++) { totalHours += WeeklyUsage[space][day]; } System.out.println("Space " + space + " total hours: " + totalHours); }
' Total hours parked for each of the 50 spaces, across all 5 days For space = 1 To 50 totalHours = 0 For day = 1 To 5 totalHours = totalHours + WeeklyUsage(space, day) Next day Console.WriteLine("Space " & space & " total hours: " & totalHours) Next space
What is the outer loop (FOR Space ← 1 TO 50) for? It moves through the 50 rows of WeeklyUsage, one space at a time: Space 1, then Space 2, and so on up to Space 50. Everything indented inside it runs once per space, including the entire inner loop below it.
外层循环(FOR Space ← 1 TO 50)的作用是什么?它逐一遍历 WeeklyUsage 的 50 行:车位 1,然后车位 2,一直到车位 50。缩进在它内部的所有内容都每个车位运行一次,包括下面整个内层循环。
What is the inner/nested loop (FOR Day ← 1 TO 5) for? For whichever space the outer loop is currently on, it moves through that space’s 5 columns, Day 1 through Day 5, adding each day’s hours to TotalHours. It runs all 5 days to completion before the outer loop is allowed to move to the next space, which is exactly why TotalHours is reset to 0 just before it starts: each space needs its own fresh total.
内层(嵌套)循环(FOR Day ← 1 TO 5)的作用是什么?对于外层循环当前所在的那个车位,它遍历该车位的 5 列,从第 1 天到第 5 天,把每天的时长加到 TotalHours 上。它会把全部 5 天全部跑完,外层循环才被允许移动到下一个车位,这正是为什么 TotalHours 要在它开始之前重置为 0:每个车位都需要一个全新的总计。
Pseudocode or program code accepted here: the 15-mark scenario question is the one place in Paper 2 where Cambridge allows Python, Java or Visual Basic instead of pseudocode. Every other Paper 2 question (validation, trace tables, error-correction) stays pseudocode-only.
此处接受伪代码或程序代码:这道 15 分的情境题是试卷二中唯一允许用 Python、Java 或 Visual Basic 替代伪代码作答的地方。试卷二的其他题目(验证、追踪表、纠错)一律只接受伪代码。
Note the order inside the brackets never changes: it’s always [Space, Day] (or [space][day] in Python/Java), matching how WeeklyUsage was declared. Swap them and you read the wrong cell.
注意方括号内的顺序始终不变:总是 [Space, Day](在 Python/Java 中是 [space][day]),与 WeeklyUsage 的声明方式一致。顺序颠倒就会读到错误的单元格。
Try it yourself
动手试试
Run the 2D array code
运行二维数组代码
The parking-hours grid from just above, live and editable. The inner loop finishes all 5 days for one space before the outer loop moves on, which is why each space gets its own fresh space_total. Try the experiment at the bottom to see what a swapped index does.
这是上面停车时长网格的实时可编辑版本。内层循环会为一个车位跑完全部 5 天,外层循环才会继续,这就是为什么每个车位都有自己全新的 space_total。试试底部的实验,看看索引颠倒会发生什么。
Running in offline fallback mode. The full Python engine couldn’t load (blocked school network, or no internet), so this console switched automatically to a small hand-written engine. It runs lists, indexing, range(), for loops, len() and nested lists, which covers every array example on this page, but it is not full Python. One visible difference: a list of text prints without quotes ([a, b] rather than ['a', 'b']).
正在以离线备用模式运行。完整的 Python 引擎未能加载(学校网络受限,或没有网络连接),因此本控制台自动切换到了一个小型的手写引擎。它支持列表、索引、range()、for 循环、len() 以及嵌套列表,足以覆盖本页的每一个数组示例,但它并非完整的 Python。一个可见的差异是:文本列表打印时不带引号(显示为 [a, b] 而非 ['a', 'b'])。
Check your understanding
检验你的理解
In Cambridge pseudocode, Space 12’s hours on Day 3 are needed. Which line reads it correctly?
在剑桥伪代码中,需要读取车位 12 在第 3 天的时长。哪一行代码正确地读取了它?
Explainer
讲解
How to trace code that indexes a 2D list
如何追踪索引二维列表的代码
Before answering a “what does this print?” question, trace it exactly like a trace table: one row per loop pass, tracking the index and the running total. Here it is worked in full on a fresh example: rainfall in mm, recorded at 3 stations over 3 months.
在回答"这段代码会打印什么?"这类问题之前,要像追踪表一样精确地追踪它:每次循环一行,记录索引和累计总数。这里以一个新例子完整演示:3 个站点、3 个月的降雨量(毫米)记录。
# RainfallMM[0] = Station A, [1] = Station B, [2] = Station C RainfallMM = [ [12, 8, 15], [20, 5, 9], [3, 11, 7] ] total = 0 for month in range(3): total = total + RainfallMM[0][month] print(total)
What is RainfallMM[0]? It’s the whole first row, [12, 8, 15]: Station A’s three months of rainfall. Read individually, that’s Month 1 = 12mm, Month 2 = 8mm, Month 3 = 15mm.
RainfallMM[0] 是什么?它是整个第一行,[12, 8, 15]:站点 A 三个月的降雨量。逐个来看,即第 1 月 = 12 毫米,第 2 月 = 8 毫米,第 3 月 = 15 毫米。
What is RainfallMM[0][month]? It refers to Station A’s rainfall in that month, whichever month the loop is currently on. The first index (0) stays fixed at Station A for the whole loop; the second index (month) is the one that changes, counting 0, 1, 2 as the loop runs.
RainfallMM[0][month] 是什么?它指的是站点 A 在那个月的降雨量,具体是哪个月取决于循环当前所在的位置。第一个索引(0)在整个循环中固定指向站点 A;第二个索引(month)才是变化的那个,随循环运行依次为 0、1、2。
| Pass | 遍数 | month | RainfallMM[0][month] | total | ||
|---|---|---|---|---|---|---|
| start | 起始 | n/a | 不适用 | n/a | 不适用 | 0 |
| 1 | 0 | 12 | 12 | |||
| 2 | 1 | 8 | 20 | |||
| 3 | 2 | 15 | 35 |
The row index (0, fixed) never changes. Only month moves, from 0 to 2. Output: 35.
行索引(0,固定)始终不变。只有 month 在变化,从 0 到 2。输出:35。
Check your understanding
检验你的理解
Same technique, different row. What does this code output?
同样的方法,不同的行。这段代码会输出什么?
# same RainfallMM as above total = 0 for month in range(3): total = total + RainfallMM[2][month] print(total)
Common mistakes
常见错误
8.2.1 / 8.2.2 / 8.2.3考官针对 8.2.1 / 8.2.2 / 8.2.3 报告了以下模式RecurringAsked why an array is an effective data structure, many candidates described what an array is instead of explaining why it helps. Naming the benefit is what earns the mark: one identifier for many related values, and an index you can move through with a loop to reach or search every element.
反复出现当被问及数组为什么是一种有效的数据结构时,许多考生描述了数组是什么,而不是解释它为什么有帮助。说明其益处才能得分:一个标识符对应多个相关的值,并可用循环遍历索引来访问或查找每一个元素。
At the declaration stage, markers repeatedly noted an identifier with a space or punctuation in it, or a name that changes partway through an answer, neither of which would run in a real program. Candidates also lost marks for not using the variable names the question supplied, or for not using array indexing where the task required it.
在声明阶段,考官多次指出标识符中带有空格或标点,或名称在答案中途发生变化,这两者在真实程序中都无法运行。考生还因未使用题目提供的变量名,或未在任务要求处使用数组索引而失分。
A third pattern is a design error: candidates who reached for two separate 1D arrays instead of the single 2D array a scenario called for, then, having built the 2D array, read it with swapped or hard-coded indices rather than both loop counters.
第三种模式是设计错误:考生使用两个独立的一维数组,而不是情境所要求的单一二维数组;而在建好二维数组之后,又用颠倒或写死的索引来读取它,而不是使用两个循环计数器。
Identifier that will not compile: DECLARE Student Mark : ARRAY[1:30] OF INTEGER. The space splits the name in two, so no program can use it.
无法编译的标识符: DECLARE Student Mark : ARRAY[1:30] OF INTEGER。空格把名称分成两半,任何程序都无法使用它。
One single word, used everywhere: DECLARE StudentMark : ARRAY[1:30] OF INTEGER, then StudentMark[Count] in every loop, never StudentMarks or Marks later in the same answer.
始终使用同一个单词: DECLARE StudentMark : ARRAY[1:30] OF INTEGER,然后在每个循环中都用 StudentMark[Count],同一份答案中后面绝不会变成 StudentMarks 或 Marks。
Describing when asked to explain: “An array is a list of boxes with numbers on them.” True, but it answers a different question and scores nothing for “why is it effective”.
被要求解释时却在描述:"数组是一列带编号的方框。"这话没错,但回答的是另一个问题,对"为什么它是有效的"这道题不得分。
Naming the benefit: “One array with an index lets the same loop reach all 30 marks, so the code does not grow with the class size and any element can be searched or totalled.”
说明其益处:"一个带索引的数组让同一个循环就能访问全部 30 个成绩,因此代码不会随班级人数增长而变多,任何元素都可以被查找或求和。"
Swapped indices: TotalHours ← TotalHours + WeeklyUsage[Day, Space]. Not a syntax error, so it is not flagged, it just silently reads the wrong cell.
索引颠倒: TotalHours ← TotalHours + WeeklyUsage[Day, Space]。这不是语法错误,不会被标记出来,只会默默地读到错误的单元格。
Declared order, every time: TotalHours ← TotalHours + WeeklyUsage[Space, Day]. The outer loop variable (Space) goes first, the inner one (Day) second.
始终按声明顺序: TotalHours ← TotalHours + WeeklyUsage[Space, Day]。外层循环变量(Space)在前,内层循环变量(Day)在后。
Go deeper
深入练习
Ready for the exam-standard version?
准备好挑战考试标准版了吗?
This whole subtopic is what the hardest Paper 2 question tests: declaring and choosing arrays, then nested iteration over 1D and 2D arrays combined with validation and accumulation. The full worked remediation (anatomy, common mistakes, scaffold and mark scheme) is already built.
这整个子主题正是试卷二最难题目所考察的内容:声明和选择数组,再结合验证与累加,对一维和二维数组进行嵌套迭代。完整的补救练习(结构剖析、常见错误、支架式引导与评分标准)已经建好。
Revision questions
复习题
Past-paper style. Write an answer, then reveal the marking points and mark yourself.
采用历年真题风格。写下答案,然后展开评分要点给自己打分。
[1]State one reason an array is a more suitable data structure than 30 separate variables for storing 30 exam marks.
[1]陈述一个理由,说明为什么数组比 30 个独立的变量更适合用来存储 30 个考试成绩。
Marking points
评分要点
- One identifier holds all 30 related values instead of 30 separate names.
- An index lets a single loop reach every mark, so the code does not grow with the class size and any element can be searched or totalled.
- 一个标识符保存全部 30 个相关的值,而不是 30 个独立的名字。
- 索引让单个循环就能访问每一个成绩,所以代码不随班级规模增长,而且任何元素都能被查找或求和。
[2]Write a declaration for an array to store one name for each of 40 club members.
[2]写出一个数组声明,用来存储 40 名俱乐部成员各自的姓名。
Marking points
评分要点
- DECLARE MemberName : ARRAY[1:40] OF STRING
- Marks for: a meaningful identifier with no spaces; ARRAY[lower:upper] with bounds giving 40 elements; OF STRING for names.
- DECLARE MemberName : ARRAY[1:40] OF STRING
- 评分点:有意义、无空格的标识符;ARRAY[下界:上界],边界给出 40 个元素;OF STRING 用于姓名。
[3]Write pseudocode using a loop to set every element of the array Totals[1:12] to 0.
[3]编写使用循环的伪代码,将数组 Totals[1:12] 的每一个元素都设为 0。
Marking points
评分要点
- FOR Index <- 1 TO 12
- Totals[Index] <- 0
- NEXT Index
- Marks for: a loop over the full index range 1 to 12, and setting Totals[Index] (not a fixed slot) to 0 inside it.
- FOR Index <- 1 TO 12
- Totals[Index] <- 0
- NEXT Index
- 评分点:遍历完整索引范围 1 到 12 的循环,并在其中把 Totals[Index](不是固定位置)设为 0。
[4]Write pseudocode using nested iteration to output every value stored in WeeklyUsage[1:50, 1:5].
[4]编写使用嵌套迭代的伪代码,输出 WeeklyUsage[1:50, 1:5] 中存储的每一个值。
Marking points
评分要点
- Outer loop over rows, inner loop over columns, in the declared order:
- FOR Space <- 1 TO 50
- FOR Day <- 1 TO 5
- OUTPUT WeeklyUsage[Space, Day]
- NEXT Day
- NEXT Space
- Marks for: correct nesting, both loop counters used as the two indices, indices in declared order (Space, Day).
- 外层循环遍历行,内层循环遍历列,按声明顺序:
- FOR Space <- 1 TO 50
- FOR Day <- 1 TO 5
- OUTPUT WeeklyUsage[Space, Day]
- NEXT Day
- NEXT Space
- 评分点:正确的嵌套、两个循环计数器都用作两个索引、索引按声明顺序(Space, Day)。
[4]A programmer writes WeeklyUsage[Day, Space] instead of WeeklyUsage[Space, Day] throughout their program. Explain the effect this has, and how to fix it.
[4]某程序员在整个程序中都写成 WeeklyUsage[Day, Space],而不是 WeeklyUsage[Space, Day]。解释这样做会产生什么影响,以及如何修正。
Marking points
评分要点
- Effect: it is not a syntax error, so nothing is flagged; the program silently reads the wrong cell every time (row and column swapped).
- With ARRAY[1:50, 1:5], WeeklyUsage[Day, Space] uses Day (1 to 5) as the row and Space (1 to 50) as the column, so most accesses are out of range or wrong.
- Fix: always index in the declared order, WeeklyUsage[Space, Day], with the outer loop counter first.
- 影响:这不是语法错误,所以不会被报错;程序每次都悄悄读错单元格(行列颠倒)。
- 对于 ARRAY[1:50, 1:5],WeeklyUsage[Day, Space] 用 Day(1 到 5)作行、Space(1 到 50)作列,所以大多数访问越界或出错。
- 修正:始终按声明顺序索引,WeeklyUsage[Space, Day],外层循环计数器在前。
Flashcards
记忆卡片
Active recall for this subtopic’s key terms.
针对本子主题关键术语的主动回忆练习。
Keep this subtopic’s practice with you
把这个子主题的练习保存下来
Free account: save your progress and unlock every revision question for 8.2, and every other IGCSE 0478 statement.
免费账户:解锁 8.2 以及所有其他 IGCSE 0478 考纲陈述的每一道复习题,并同步你的学习进度。