Arrays in IGCSE CCEA Computer Science: Key Concepts | IGCSE CCEA 计算机:数组 考点精讲

📚 Arrays in IGCSE CCEA Computer Science: Key Concepts | IGCSE CCEA 计算机:数组 考点精讲

Arrays are one of the fundamental data structures you will encounter in IGCSE CCEA Computer Science. Understanding how arrays store multiple values under a single identifier, how indexing works, and how to manipulate array elements using loops is essential for both the written paper and programming tasks. This guide covers the key concepts, common algorithms such as linear search and bubble sort, two-dimensional arrays, and practical exam tips to help you master arrays with confidence.

数组是 IGCSE CCEA 计算机科学中最基础的数据结构之一。理解数组如何在单一标识符下存储多个值、索引如何工作、以及如何使用循环操控数组元素,对于笔试和编程任务都至关重要。本指南将涵盖关键概念、线性搜索和冒泡排序等常见算法、二维数组以及实用的考试技巧,帮助你自信掌握数组。

1. Definition and Basic Concepts of Arrays | 数组的定义与基本概念

An array is a data structure that can hold a fixed number of elements, all of the same data type, under a single variable name. Instead of declaring multiple individual variables such as mark1, mark2, mark3, you declare one array marks[3] that stores three related values. This makes code more organised and efficient, especially when dealing with large sets of data like student marks, sensor readings, or inventory items.

数组是一种数据结构,可以在一个变量名下存储固定数量的、数据类型相同的元素。与声明多个独立变量(如 mark1, mark2, mark3)不同,你可以声明一个数组 marks[3] 来存储三个相关的值。这样可使代码更有序、更高效,尤其当处理学生成绩、传感器读数或库存物品等大量数据时。

In CCEA examinations, arrays are typically declared with a specific size, and each element can be accessed using an index. The index is an integer value that represents the position of the element within the array. It is important to remember that most programming languages, including those used in pseudocode for CCEA, use zero-based indexing. However, some exam questions may use one-based indexing depending on the context, so you must read the question carefully.

在 CCEA 考试中,数组通常以指定大小声明,每个元素可通过索引访问。索引是一个整数,表示元素在数组中的位置。重要的是要记住,大多数编程语言(包括 CCEA 伪代码中使用的语言)都采用从零开始的索引。然而,有些考题可能根据上下文使用从一开始的索引,因此你必须仔细阅读题目。

The main characteristics of an array include static size (cannot be resized during runtime in classic array implementation), homogeneous data type (all elements must be of the same type, e.g., all integers or all strings), and random access (any element can be accessed directly via its index). These features make arrays predictable and fast for reading data.

数组的主要特征包括静态大小(在经典数组实现中,运行时无法调整大小)、同质数据类型(所有元素类型必须相同,例如全为整数或全为字符串)以及随机访问(任何元素都可以通过索引直接访问)。这些特性使得数组在数据读取时具有可预测性和高速性。


2. Array Indexing and Accessing Elements | 数组的索引与元素访问

Indexing is the mechanism by which you retrieve or modify a specific element in an array. In the pseudocode used by CCEA, the first element is usually at index 0. For an array score[5], valid indices are 0, 1, 2, 3, and 4. Accessing score[0] gives the first value, and score[4] gives the last value. An attempt to access an index outside this range results in an ‘array index out of bounds’ error — a common logical error in programming.

索引是检索或修改数组中特定元素的机制。在 CCEA 使用的伪代码中,第一个元素的索引通常为 0。对于数组 score[5],有效索引为 0、1、2、3 和 4。访问 score[0] 得到第一个值,score[4] 得到最后一个值。尝试访问此范围外的索引将导致“数组索引越界”错误——这是编程中常见的逻辑错误。

You can assign a value to an array element directly: score[2] ← 85 assigns the value 85 to the element at index 2. Similarly, you can output the value stored at index 2 by using OUTPUT score[2]. In many exam questions, you will be asked to trace code that modifies array elements through indexing, so it is crucial to visualise the state of the array after each assignment.

你可以直接为数组元素赋值:score[2] ← 85 将值 85 赋给索引为 2 的元素。同样,可以使用 OUTPUT score[2] 输出存储在索引 2 处的值。在许多考题中,你会被要求追踪通过索引修改数组元素的代码,因此在每次赋值后想象数组的状态至关重要。


3. Declaring and Initialising One-Dimensional Arrays | 一维数组的声明与初始化

A one-dimensional array is a simple list of elements. In pseudocode, declaration and initialisation often occur together. A typical declaration might be: DECLARE temperatures : ARRAY[1..7] OF REAL. This creates an array with 7 elements intended to hold floating-point numbers. Alternatively, the size can be expressed as ARRAY[0..6] depending on the required range. Some questions may ask you to initialise an array with specific values, e.g., names ← ["Ali", "Bella", "Chris"].

一维数组是一种简单的元素列表。在伪代码中,声明和初始化通常同时进行。一个典型的声明可能是:DECLARE temperatures : ARRAY[1..7] OF REAL。这会创建一个包含 7 个元素的数组,用于存放浮点数。或者,也可以根据所需范围将大小表示为 ARRAY[0..6]。有些题目可能要求你用特定值初始化数组,例如 names ← ["Ali", "Bella", "Chris"]

Initialisation can also be done using a loop. For example, to set all elements of an integer array nums of size 10 to zero, you would write a loop from 0 to 9 and set nums[i] ← 0. This is a common pattern in programming and ensures the array does not contain any unpredictable garbage values from previous memory usage.

初始化也可以通过循环来完成。例如,要将整数数组 nums(大小为 10)的所有元素设置为零,你可以从 0 到 9 编写一个循环,并设置 nums[i] ← 0。这是编程中的常见模式,可确保数组不包含先前内存使用留下的任何不可预测的垃圾值。


4. Traversing Arrays: Looping Structures | 遍历数组:循环结构

Traversal means visiting each element of an array, usually to read, modify, or perform calculations. The most common loop used is the FOR loop, as it allows you to control the index variable precisely. A simple traversal to output all values might look like:

遍历意味着访问数组的每个元素,通常是为了读取、修改或执行计算。最常用的循环是 FOR 循环,因为它允许你精确控制索引变量。一个简单的输出所有值的遍历可能如下:

FOR i ← 0 TO 4
  OUTPUT arr[i]
ENDFOR

This prints each element from index 0 to 4. The upper bound of the loop must match the last valid index of the array. A frequent mistake is to loop one time too many (TO 5 for an array of size 5), which causes an out-of-bounds error.

这会打印从索引 0 到 4 的每个元素。循环的上界必须与数组的最后一个有效索引匹配。一个常见错误是多循环一次(对于大小为 5 的数组循环 TO 5),这会导致越界错误。

When you need to traverse an array without modifying its elements, a FOR EACH construct can be used in some pseudocode styles. However, CCEA typically sticks to indexed FOR loops. You should also be comfortable with WHILE loops for situations where you might stop early (e.g., searching for an element and breaking out when found).

当你需要在不修改元素的情况下遍历数组时,某些伪代码风格可以使用 FOR EACH 结构。然而,CCEA 通常采用带索引的 FOR 循环。你还应熟练使用 WHILE 循环,以便在某些需要提前停止的情况下使用(例如,搜索某个元素并在找到时跳出)。


5. Common Array Operations: Summation and Searching | 常见数组操作:求和与查找

Summing all elements in an array is a classic task. You declare a variable to accumulate the total (initially zero) and then loop through the array adding each element to the total. After the loop, the variable holds the sum. A pseudocode example for an array numbers[5]:

求数组中所有元素的和是一项经典任务。你需要声明一个变量来累加总和(初始为零),然后遍历数组,将每个元素加到总和中。循环结束后,该变量中保存的就是总和。以下是一个数组 numbers[5] 的伪代码示例:

total ← 0
FOR i ← 0 TO 4
  total ← total + numbers[i]
ENDFOR
OUTPUT total

Searching for a specific value is another fundamental operation. You compare each array element with the target value. As soon as a match is found, you can output the index or a message. The simplest search algorithm is the linear search, which scans elements sequentially from the first index to the last.

查找特定值是另一项基本操作。你将每个数组元素与目标值进行比较。一旦找到匹配项,就可以输出索引或一条消息。最简单的搜索算法是线性搜索,它从第一个索引开始,依次向后扫描元素,直到找到目标。


6. Linear Search Algorithm | 线性搜索算法

The linear search algorithm examines each element one by one until the desired item is found or the end of the array is reached. In the worst case, the target is at the last position or not present at all, resulting in N comparisons for an array of size N. Despite being inefficient for large datasets, it is straightforward and does not require the array to be sorted beforehand.

线性搜索算法逐一检查每个元素,直到找到所需项或到达数组末尾。在最坏情况下,目标位于最后位置或完全不存在,对于大小为 N 的数组需要 N 次比较。尽管对于大数据集效率不高,但该算法简单直接,且不要求数组事先排序。

In pseudocode, a linear search for a target key in array data of size 10 might be written as:

在伪代码中,在大小为 10 的数组 data 中对目标 key 进行线性搜索可写成:

found ← FALSE
FOR i ← 0 TO 9
  IF data[i] = key THEN
    OUTPUT “Found at index “, i
    found ← TRUE
  ENDIF
ENDFOR
IF found = FALSE THEN
  OUTPUT “Not found”
ENDIF

In CCEA exams, you may be asked to complete a trace table or write the algorithm in pseudocode. Remember to include a flag variable (like found) to track whether the search was successful.

在 CCEA 考试中,你可能需要完成追踪表或用伪代码编写算法。记住要包含一个标志变量(如 found)来跟踪搜索是否成功。


7. Bubble Sort Algorithm | 冒泡排序算法

Bubble sort is the most common sorting algorithm tested in IGCSE CCEA. It repeatedly steps through the array, compares adjacent elements, and swaps them if they are in the wrong order. This process is repeated until the entire array is sorted. For an array of N elements, a maximum of N−1 passes are needed. In the first pass, the largest element ‘bubbles up’ to the last position; the second pass places the second largest in the second last position, and so on.

冒泡排序是 IGCSE CCEA 中最常考查的排序算法。它反复遍历数组,比较相邻元素,如果顺序错误就交换它们。重复此过程,直到整个数组有序。对于 N 个元素的数组,最多需要 N−1 趟。第一趟中,最大元素“冒泡”到最后一个位置;第二趟将第二大的元素放到倒数第二的位置,依此类推。

A standard bubble sort pseudocode for array nums[0..4] looks like:

数组 nums[0..4] 的标准冒泡排序伪代码如下:

FOR pass ← 0 TO 3
  FOR i ← 0 TO 3 – pass
    IF nums[i] > nums[i+1] THEN
      temp ← nums[i]
      nums[i] ← nums[i+1]
      nums[i+1] ← temp
    ENDIF
  ENDFOR
ENDFOR

The inner loop limit 3 - pass ensures we do not compare already sorted elements at the end. Pay attention to the swapping mechanism: three assignment statements using a temporary variable are required.

内循环限制 3 - pass 确保我们不比较末尾已排好的元素。注意交换机制:需要使用一个临时变量执行三次赋值语句。


8. Two-Dimensional Arrays | 二维数组

A two-dimensional array can be visualised as a table with rows and columns. It is declared as ARRAY[0..4, 0..2] OF INTEGER for a 5×3 grid. Each element is accessed using two indices: the first for the row, the second for the column. For example, grid[1, 2] refers to the element in row 1, column 2. This structure is ideal for representing a board game, a timetable, or a matrix in mathematics.

二维数组可以想象为一个有行和列的表格。它声明为 ARRAY[0..4, 0..2] OF INTEGER,表示一个 5×3 的网格。每个元素通过两个索引访问:第一个表示行,第二个表示列。例如,grid[1, 2] 引用第 1 行第 2 列的元素。这种结构非常适合表示棋盘游戏、时间表或数学中的矩阵。

Initialisation of a 2D array can be done using nested loops. The outer loop iterates over rows, and the inner loop iterates over columns of the current row. For instance, to set all elements of a 3×3 array matrix to 0:

二维数组的初始化可以通过嵌套循环完成。外层循环遍历行,内层循环遍历当前行的列。例如,将一个 3×3 数组 matrix 的所有元素设置为 0:

FOR r ← 0 TO 2
  FOR c ← 0 TO 2
    matrix[r, c] ← 0
  ENDFOR
ENDFOR

Exam questions often require you to write code that processes only a specific row or column, such as summing the values in a particular row.

考试题常要求你编写只处理特定行或列的代码,例如计算某一行的总和。


9. Traversing Two-Dimensional Arrays | 二维数组的遍历

To traverse every element of a 2D array, you must use nested loops. The standard pattern is to use row as the outer loop variable and col as the inner loop variable. This processes the array in row-major order (left to right, top to bottom). This is the natural order for most problems, such as printing a timetable or summing all values.

要遍历二维数组的每个元素,必须使用嵌套循环。标准模式是使用 row 作为外循环变量,col 作为内循环变量。这会以行主序(从左到右、从上到下)处理数组。这是大多数问题的自然顺序,例如打印时间表或计算所有值的总和。

Occasionally, you may need column-major traversal (top to bottom, left to right), which requires swapping the loops so that the column loop is outer and the row loop is inner. When answering exam questions, identify the required traversal order carefully. Also, keep track of the bounds: for an array ARRAY[1..m, 1..n], the row loop runs from 1 to m, and the column loop runs from 1 to n.

有时,你可能需要进行列主序遍历(从上到下、从左到右),这需要交换循环,使列循环在外、行循环在内。回答考题时,要仔细确定所需的遍历顺序。同时,注意边界范围:对于数组 ARRAY[1..m, 1..n],行循环从 1 运行到 m,列循环从 1 运行到 n。


10. Practical Applications of Arrays in Programming | 数组在编程中的实际应用

Arrays are everywhere in programming. They are used to store lists of student names, daily temperatures, high scores in a game, and much more. In CCEA project tasks, you might use an array to hold validated user inputs or to implement a simple menu system where options are stored in an array. Understanding how to use arrays efficiently will make your programs more robust and easier to debug.

数组在编程中无处不在。它们用于存储学生名单、每日温度、游戏高分等等。在 CCEA 项目任务中,你可以使用数组来保存经过验证的用户输入,或实现一个简单的菜单系统,其中选项存储在一个数组中。了解如何高效使用数组将使你的程序更健壮、更易于调试。

Another common application is searching and sorting records. For example, a program might store product IDs in one array and corresponding stock levels in another parallel array. By searching for a product ID, you can update its stock level. In such scenarios, it is crucial that the indices of related items in different arrays are correctly aligned. This technique appears frequently in exam scenarios involving parallel arrays.

另一种常见应用是搜索和排序记录。例如,一个程序可能将产品 ID 存储在一个数组中,将相应的库存数量存储在另一个并行数组中。通过搜索产品 ID,你可以更新其库存数量。在此类场景中,不同数组中相关项的索引正确对齐至关重要。这种技术经常出现在涉及并行数组的考试情景中。


11. Common Errors and Debugging Tips | 常见错误与调试技巧

One of the most frequent mistakes is an off-by-one error in loops. For an array of size 5 with indices 0 to 4, writing FOR i ← 0 TO 5 will cause a runtime error when i = 5. Always double-check that the loop upper bound equals the highest valid index. Another typical error is forgetting to initialise the array or using incorrect data types for elements (e.g., storing a string in an integer array).

最常见的错误之一是循环中的差一错误。对于大小为 5、索引为 0 到 4 的数组,编写 FOR i ← 0 TO 5 会在 i = 5 时引发运行时错误。务必仔细检查循环上界是否等于最大有效索引。另一个典型错误是忘记初始化数组,或对元素使用了不正确的数据类型(例如,在整数数组中存储字符串)。

Debugging array code requires systematic tracing. Use a trace table to track the values of index variables and key array elements at each step. Watch for unintended modification of the index variable inside the loop, and ensure swap routines use a temporary variable correctly. In CCEA written exams, tracing is a core skill, and practice will help you avoid careless mistakes.

调试数组代码需要系统化的追踪。使用追踪表在每一步跟踪索引变量和关键数组元素的值。注意循环内部索引变量的意外修改,并确保交换程序正确使用临时变量。在 CCEA 笔试中,追踪是一项核心技能,多加练习将帮助你避免粗心错误。


12. Exam Tips for Array Questions | 数组考题答题技巧

When facing a CCEA array question, first identify the array’s size and index range. Highlight whether it is zero-based or one-based from the question context. Read all parts of the question carefully — sometimes you are given a partially filled array and need to complete it, or you need to identify the output of a given algorithm. Use rough work to draw the array and update its contents step by step as you trace.

面对 CCEA 数组考题时,首先要确定数组的大小和索引范围。根据题目上下文,标出它是从零开始还是从一开始。仔细阅读题目的所有部分——有时你会得到一个部分填充的数组并需要补全,或者你需要确定给定算法的输出。使用草稿纸画出数组,并在追踪时逐步更新其内容。

For algorithm writing questions, pseudo-code syntax must be clear and consistent. Use indentation to show loop bodies, and ensure all variables are declared. Common algorithms like linear search and bubble sort should be memorised, but also understand the logic so you can adapt them if the question modifies the condition slightly (e.g., sorting in descending order). Finally, allocate time to check your loops and bounds—an extra minute of verification can save several marks lost to a simple indexing error.

对于算法编写题,伪代码语法必须清晰一致。使用缩进显示循环体,并确保所有变量都已声明。像线性搜索和冒泡排序这样的常见算法应当熟记,但也要理解其逻辑,以便在题目稍微修改条件时(例如,按降序排序)能够灵活调整。最后,留出时间检查循环和边界——多花一分钟验证可以避免因简单的索引错误而丢失好几分。

Published by TutorHao | IGCSE CCEA Computer Science Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading