📚 IGCSE CIE Computer Science: Arrays | IGCSE CIE 计算机:数组 考点精讲
Arrays are one of the most important data structures you will encounter in IGCSE Computer Science. They allow a programmer to store and manipulate a collection of data items of the same type under a single variable name. Understanding how to declare, traverse, search, and sort arrays is essential for tackling both paper-based pseudocode questions and practical programming problems in the CIE syllabus (0478/0984). This article breaks down every key concept you need, with clear pseudocode examples and exam tips.
数组是 IGCSE 计算机科学中最关键的数据结构之一。它使程序员能够在单个变量名下存储并操作一组相同类型的数据项。透彻理解数组的声明、遍历、搜索和排序,对于应对 CIE 考纲 (0478/0984) 中的伪代码笔试和实践编程题都至关重要。本文逐一拆解所有核心概念,配以清晰的伪代码示例与应试技巧。
1. What is an Array? | 什么是数组?
An array is a static data structure that holds a fixed number of values, all of the same data type (e.g. INTEGER, STRING). Each value is called an element, and every element can be accessed directly using its index. Because the size is defined when the array is declared, you cannot easily add extra elements beyond the declared bound – this is a key characteristic of arrays in the CIE syllabus.
数组是一种静态数据结构,能存放固定数量的值,且所有值必须属于同一数据类型(例如 INTEGER 或 STRING)。其中每个值称为元素,每个元素都可以通过其索引直接访问。由于数组的大小在声明时就已确定,因此无法轻易在声明边界之外添加额外元素——这是 CIE 考纲中数组的一个重要特征。
In pseudocode, you might see a declaration like DECLARE numbers : ARRAY[1:10] OF INTEGER. This creates an array named ‘numbers’ with index positions 1 through 10, ready to store ten integers. Arrays are widely used when a program needs to process lists of data such as test scores, names, or sensor readings.
在伪代码中,你可能会看到类似 DECLARE numbers : ARRAY[1:10] OF INTEGER 的声明。这会创建一个名为 ‘numbers’ 的数组,索引位置从 1 到 10,共可存放十个整数。当程序需要处理一系列数据(如测验分数、姓名或传感器读数)时,数组就会被广泛使用。
2. One-Dimensional and Two-Dimensional Arrays | 一维与二维数组
A one-dimensional (1D) array is a simple list of elements. You reference an element by a single index, such as scores[4]. A two-dimensional (2D) array can be thought of as a table with rows and columns; it requires two indices, for example grid[2,3]. In CIE pseudocode, a 2D array is declared using two ranges: DECLARE table : ARRAY[1:3,1:4] OF STRING declares a table with 3 rows and 4 columns.
一维(1D)数组就是一个简单的元素列表。你只需使用单个索引来引用元素,例如 scores[4]。二维(2D)数组则可以看作一个由行列构成的表格,它需要两个索引,如 grid[2,3]。在 CIE 伪代码中,二维数组使用两个范围来声明:DECLARE table : ARRAY[1:3,1:4] OF STRING 就声明了一个具有 3 行 4 列的表格。
1D arrays commonly appear in tasks like storing a list of temperatures for the last seven days. 2D arrays are suitable for representing a game board, a seating plan, or a multiplication table. You must be able to read, write, and manipulate both types using nested loops.
一维数组常见于存储过去七天的温度列表等任务。二维数组则适合表示棋盘、座位图或乘法表。你必须能够运用嵌套循环来读取、写入并操作这两种数组。
3. Array Indexing | 数组索引
The index (or subscript) indicates an element’s position within the array. CIE pseudocode often uses declared lower and upper bounds, for example [0:4] means indices 0,1,2,3,4. Note that the first index can be 0 or 1 depending on the declaration – always read the given bounds carefully. The last valid index is the upper bound, so an array declared as [0:9] has 10 elements, with the final element at index 9.
索引(或下标)指明元素在数组中的位置。CIE 伪代码通常使用声明的下界和上界,例如 [0:4] 表示索引为 0、1、2、3、4。注意,第一个索引可以是 0 也可以从 1 开始,具体取决于声明——务必仔细阅读题目给定的边界。最后一个有效索引即上界,因此声明为 [0:9] 的数组共有 10 个元素,末尾元素的索引是 9。
Attempting to access an index outside the declared range results in an ‘index out of bounds’ error. This is a common logic mistake; always ensure your loop counters stay within the valid limits. For a 1D array arr[0:5], a FOR loop should run from 0 to 5 (inclusive) or use LENGTH(arr)-1 in languages that use zero-based default indexing.
试图访问声明范围之外的索引将导致“索引越界”错误。这是一种常见的逻辑错误;务必确保循环计数器始终保持在有效范围之内。对于一维数组 arr[0:5],FOR 循环应从 0 运行至 5(含),或在默认以零为基础的编程语言中使用 LENGTH(arr)-1。
4. Declaring and Initialising Arrays | 声明与初始化数组
In IGCSE pseudocode, you declare an array by specifying its name, bounds, and data type. Example: DECLARE studentNames : ARRAY[1:30] OF STRING. You can initialise elements one by one: studentNames[1] ← “Alice”. Alternatively, you may be asked to fill the array using a loop, e.g. reading values from user input.
在 IGCSE 伪代码中,你通过指定名称、边界和数据类型来声明数组。例如:DECLARE studentNames : ARRAY[1:30] OF STRING。你可以逐个初始化元素:studentNames[1] ← “Alice”。此外,也可能要求使用循环来填充数组,例如从用户输入读取数值。
Some questions provide an initialiser list for convenience, such as PRICE ← [5.99, 2.49, 8.79]. However, the syllabus emphasises understanding explicit declaration. Always remember that once declared, the array size is fixed; you cannot dynamically add elements. If you need a resizable collection, the syllabus expects you to appreciate the static nature of arrays.
部分题目会以简化形式给出初始化列表,如 PRICE ← [5.99, 2.49, 8.79]。但考纲更强调对显式声明的理解。始终记住,数组一旦声明,其大小就固定了;你不能动态添加元素。若需可变大小的集合,考纲希望你能意识到数组的静态特性。
5. Traversing an Array | 遍历数组
Traversal means visiting every element of an array, typically with a loop. The most common approach is a FOR loop that uses the index to access each element in order. For an array declared as temps[0:6], a traversal to output all values looks like: FOR i ← 0 TO 6 OUTPUT temps[i] NEXT i.
遍历是指访问数组中的每一个元素,通常借助循环来完成。最常见的方法是使用 FOR 循环,按索引依次访问每个元素。对于一个声明为 temps[0:6] 的数组,输出所有值的遍历过程可以写作:FOR i ← 0 TO 6 OUTPUT temps[i] NEXT i。
You can also traverse to modify elements, compute a running total, or find the maximum. For instance, maxVal ← temps[0] followed by a loop comparing each element and updating maxVal when a larger value is found. Traversals form the backbone of all array-based algorithms in the CIE examination.
你还可以在遍历中修改元素、计算累计总和或找出最大值。例如,先设 maxVal ← temps[0],然后循环比较每个元素,并在发现更大值时更新 maxVal。遍历构成了 CIE 考试中所有基于数组的算法的核心。
6. Searching with Arrays | 使用数组进行搜索
Two search algorithms are explicitly tested: linear search and binary search. Linear search checks each element sequentially from the first index until the target is found or the end is reached. It works on unsorted arrays and is simple to implement. Pseudocode: FOR i ← 0 TO n-1 IF arr[i] = target THEN OUTPUT i ENDIF NEXT i.
考试明确考查两种搜索算法:线性搜索和二分搜索。线性搜索从第一个索引开始逐一检查每个元素,直到找到目标或到达末尾。它可用于无序数组,且实现简单。伪代码为:FOR i ← 0 TO n-1 IF arr[i] = target THEN OUTPUT i ENDIF NEXT i。
Binary search, on the other hand, requires the array to be sorted. It repeatedly divides the search interval in half by comparing the target with the middle element. If the target is less than the middle, the search continues on the lower half; if greater, on the upper half. This reduces the number of comparisons drastically, with a worst-case time complexity of O(log n).
二分搜索则要求数组必须为有序。它通过将目标与中间元素进行比较,反复将搜索区间减半。若目标小于中间值,则在左半区继续搜索;若大于,则在右半区继续。这大幅减少了比较次数,最坏情况时间复杂度为 O(log n)。
In CIE pseudocode, you need to manage variables low, high, and mid. The loop condition is WHILE low ≤ high. Calculate mid ← (low + high) DIV 2. Then adjust low ← mid + 1 or high ← mid – 1 based on comparison. Remember to output ‘not found’ if the loop ends without finding the target.
在 CIE 伪代码中,你需要管理变量 low、high 和 mid。循环条件为 WHILE low ≤ high。计算 mid ← (low + high) DIV 2。然后根据比较结果调整 low ← mid + 1 或 high ← mid – 1。若循环结束仍未找到目标,切记输出“未找到”。
7. Sorting Arrays | 数组排序
Bubble sort is the primary sorting algorithm you must understand. It works by repeatedly stepping through the array, comparing adjacent elements and swapping them if they are in the wrong order. After each full pass, the largest unsorted element ‘bubbles’ to its correct position at the end of the array.
冒泡排序是你必须掌握的主要排序算法。它通过反复遍历数组,比较相邻元素并在顺序错误时交换它们。每完成一轮完整扫描,当前未排序部分的最大元素就会“冒泡”到数组末尾的正确位置。
A typical CIE pseudocode structure involves an outer loop from i ← 0 TO n-2 and an inner loop from j ← 0 TO n-i-2. Inside the inner loop, an IF arr[j] > arr[j+1] condition triggers a swap using a temporary variable: temp ← arr[j]; arr[j] ← arr[j+1]; arr[j+1] ← temp. A common optimisation is to introduce a flag to detect if any swap occurred; if no swap happens during a pass, the array is already sorted and the algorithm can stop early.
典型的 CIE 伪代码结构包含一个从 i ← 0 TO n-2 的外层循环,和一个从 j ← 0 TO n-i-2 的内层循环。在内层循环中,当 IF arr[j] > arr[j+1] 时使用临时变量进行交换:temp ← arr[j]; arr[j] ← arr[j+1]; arr[j+1] ← temp。常见的优化是引入一个标志位来检测是否发生过交换;若某轮扫描未发生任何交换,则数组已有序,算法可提前终止。
8. Using 2D Arrays | 二维数组的应用
2D arrays are processed with nested loops: the outer loop iterates over rows, the inner loop over columns. To initialise a 3×4 grid of zeros, you could write: FOR row ← 1 TO 3 FOR col ← 1 TO 4 grid[row,col] ← 0 NEXT col NEXT row. This pattern appears frequently in practical programming questions.
使用嵌套循环处理二维数组:外层循环遍历行,内层循环遍历列。若要初始化一个 3×4 的零值网格,可以写作:FOR row ← 1 TO 3 FOR col ← 1 TO 4 grid[row,col] ← 0 NEXT col NEXT row。这种模式频繁出现在实践编程题中。
Below is a simple representation of a 2D array storing a times table. Access table[2,3] would retrieve the value 6, since row 2, column 3 stores 2×3.
下面是一个用于存放乘法表的二维数组的简单示意。访问 table[2,3] 将取得值 6,因为第 2 行第 3 列存放着 2×3。
| Col 1 | Col 2 | Col 3 | |
|---|---|---|---|
| Row 1 | 1 | 2 | 3 |
| Row 2 | 2 | 4 | 6 |
| Row 3 | 3 | 6 | 9 |
You should be comfortable converting between (row, column) indices and the data they point to. Also, note that in pseudocode the first index is usually the row, and the second is the column – array[row, col]. Questions may ask you to sum rows, count specific values, or find the highest score in a student-subject table.
你应当能熟练转换(行,列)索引与其指向的数据。还要留意,在伪代码中第一个索引通常表示行,第二个表示列——即 array[row, col]。考题可能要求你对各行的值求和、统计特定值出现的次数,或在学生-科目表格中找出最高分。
9. Common Errors and Debugging | 常见错误与调试
Off-by-one errors are the most frequent mistake: writing FOR i ← 0 TO n when the valid indices are 0 to n-1, causing an out-of-bounds access. Always double-check the declared bounds. Another typical error is forgetting to initialise array elements; reading an uninitialised element gives unpredictable results.
“偏移一位”错误是最常见的失误:当有效索引是 0 至 n-1 时,却写成了 FOR i ← 0 TO n,从而导致越界访问。务必反复核对声明的边界。另一个典型错误是忘记初始化数组元素;读取未初始化的元素将产生不可预测的结果。
When working with 2D arrays, confusing rows and columns can lead to logic bugs. Make sure your outer loop variable maps to the row index, and your inner loop variable maps to the column index consistently. In bubble sort, an incorrect inner loop boundary (j ← 0 TO n-1 instead of n-i-2) will cause repeated comparisons and possibly swaps beyond the already sorted portion, degrading efficiency.
在处理二维数组时,混淆行列会导致逻辑错误。请确保外层循环变量始终对应行索引,内层变量对应列索引。在冒泡排序中,错误的内层循环边界(j ← 0 TO n-1 而非 n-i-2)将导致对已排序区域重复比较乃至交换,降低效率。
Always test your pseudocode with a small trace table. Walk through the algorithm manually to verify that indices and conditions behave as expected. The exam often rewards clear, well-structured solutions even if minor syntactical liberties are taken.
务必用小型表格手动追踪你的伪代码。逐步执行算法以验证索引和条件是否符合预期。即便在句法上略有自由发挥,考试通常仍奖励清晰、结构良好的解答。
10. Exam Tips and Example Questions | 考试技巧与真题示例
Read the question carefully to identify the array bounds, data type, and required algorithm. If asked to write pseudocode for a linear search that counts occurrences, initialise a counter to zero and increment it each time the target matches. Always use meaningful variable names and proper indentation – this makes your logic easier to follow.
仔细审题,明确数组边界、数据类型和所需的算法。若题目要求写出统计出现次数的线性搜索伪代码,就将计数器初始化为零,并在每次匹配目标时使其递增。始终使用有意义的变量名并保持适当缩进——这会让你的逻辑更易理解。
For example, a past paper might ask: “An array marks[1:30] stores integer test scores. Write an algorithm to calculate and output the average score.” You begin with total ← 0, traverse FOR i ← 1 TO 30 total ← total + marks[i] NEXT i, then output total / 30. For an algorithm that finds the maximum, initialise maxM ← marks[1] and inside a loop use IF marks[i] > maxM THEN maxM ← marks[i].
举个例子,某道历年真题可能问:“数组 marks[1:30] 存储整数测验分数。编写算法计算并输出平均分。”你应设 total ← 0,遍历 FOR i ← 1 TO 30 total ← total + marks[i] NEXT i,然后输出 total / 30。对于寻找最大值的算法,则初始化 maxM ← marks[1],并在循环内使用 IF marks[i] > maxM THEN maxM ← marks[i]。
Practice writing bubble sort and binary search from memory, as these are frequently examined. Keep in mind that CIE mark schemes look for correct use of array indexing, appropriate loop constructions, and accurate logic; straying from the expected algorithm can lose marks even if the outcome appears correct.
反复练习默写冒泡排序和二分搜索,因为它们考查频率很高。记住,CIE 评分标准看重正确的数组索引使用、恰当的循环结构以及准确的逻辑;即使结果看似正确,偏离预期算法也可能失分。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导