📚 IGCSE Edexcel Computer Science: Arrays – Key Exam Points | IGCSE Edexcel 计算机科学:数组 考点精讲
Arrays form the backbone of many algorithms tested in IGCSE Edexcel Computer Science. Whether you’re searching for a value, processing numerical data, or laying out a 2D game grid, a solid grasp of array declaration, indexing, traversal, and common operations is vital. This article breaks down the key concepts, typical pseudocode patterns, and exam pitfalls so you can approach array questions with confidence.
数组是 IGCSE Edexcel 计算机科学中许多算法测试的基础。无论你是在搜索某个值、处理数值数据,还是布置二维游戏网格,扎实掌握数组的声明、索引、遍历和常见操作都至关重要。本文分解关键概念、典型伪代码模式和考试陷阱,帮助你有信心地应对数组题目。
1. What are Arrays? | 什么是数组?
An array is a data structure that holds a fixed number of elements of the same data type in contiguous memory locations. Each element is identified by an index (or subscript), allowing direct access without having to iterate from the beginning every time.
数组是一种数据结构,它在连续的内存位置中保存固定数量的、相同数据类型的元素。每个元素都由一个索引(下标)标识,允许直接访问而无需每次都从头开始迭代。
In Edexcel IGCSE pseudocode, arrays are zero-indexed, meaning the first element is at index 0 and the last at LEN(arr)-1. Most algorithms assume this, but always check the question’s wording — some older contexts might use 1-based indexing, though unlikely in current exams.
在 Edexcel IGCSE 伪代码中,数组索引从 0 开始,即第一个元素位于索引 0,最后一个位于 LEN(arr)-1。大多数算法都默认这一点,但务必检查题目的措辞——某些旧场景可能使用基于 1 的索引,不过在现行考试中可能性很小。
2. Declaring and Initialising Arrays | 声明与初始化
A one-dimensional array is declared using the syntax: DECLARE identifier : ARRAY[0:size-1] OF DataType. For instance, DECLARE scores : ARRAY[0:4] OF INTEGER reserves space for five integers. The size cannot be changed later — this is a static array.
声明一维数组的语法是:DECLARE 标识符 : ARRAY[0:大小-1] OF 数据类型。例如,DECLARE scores : ARRAY[0:4] OF INTEGER 预留了五个整数的空间。这个大小之后不可更改——这就是静态数组。
You can initialise individual elements with assignment statements: scores[0] ← 85, scores[1] ← 92, and so on. A loop is handy for bulk initialisation, e.g. reading values from user input. Some versions also support an initialiser list like scores ← [85, 92, 78, 65, 88], but exam pseudocode typically expects step-by-step assignment.
你可以用赋值语句初始化各个元素:scores[0] ← 85、scores[1] ← 92,等等。循环在批量初始化时非常方便,例如从用户输入读取数值。某些版本也支持初始化列表,如 scores ← [85, 92, 78, 65, 88],但考试伪代码通常要求逐步赋值。
3. Accessing Array Elements by Index | 索引访问
To read an element, you use variableName ← arrayName[index]. For example, firstScore ← scores[0] retrieves the first value. To update an existing element: scores[2] ← 90 overwrites the third element.
要读取一个元素,使用 变量名 ← 数组名[索引]。例如,firstScore ← scores[0] 获取第一个值。要更新已有元素:scores[2] ← 90 覆盖第三个元素。
A common mistake is accessing an index outside the declared range, such as scores[5] when the highest valid index is 4. This causes a runtime error (“index out of bounds”). Always keep the array bounds in mind during loop traversals.
一个常见错误是访问超出声明范围的索引,比如当最高有效索引是 4 时使用 scores[5]。这会导致运行时错误(“索引越界”)。在循环遍历时务必时刻注意数组边界。
4. Traversing Arrays with Loops | 循环遍历
Traversal means visiting each element of the array, usually to display, modify, or accumulate data. The most common pattern uses a FOR loop with an index variable from 0 to LEN(arr)-1:
遍历是指访问数组的每个元素,通常用于显示、修改或累加数据。最常见的模式是使用一个 FOR 循环,索引变量从 0 到 LEN(arr)-1:
FOR i ← 0 TO LEN(scores)-1 OUTPUT scores[i]NEXT i
You can replace OUTPUT with any operation, such as adding to a total or comparing against a search key. LEN() returns the number of elements, so the loop covers all indices exactly once. Using LEN() rather than a magic number makes code reusable for different array lengths.
你可以用任何操作替换 OUTPUT,比如累加到总和或与搜索关键字比较。LEN() 返回元素个数,因此循环恰好覆盖所有索引一次。使用 LEN() 而不是固定数字能让代码适用于不同长度的数组。
5. Common Array Operations | 常见操作
In exams, you are frequently asked to compute the sum, count, mean, maximum, or minimum of array elements. These typically combine a traversal with a variable initialised before the loop.
考试中,常常要求计算数组元素的总和、计数、平均值、最大值或最小值。这些操作通常将遍历与在循环前初始化的变量结合起来。
For instance, to find the maximum value:
例如,找最大值:
maxVal ← scores[0]FOR i ← 1 TO LEN(scores)-1 IF scores[i] > maxVal THEN maxVal ← scores[i] ENDIFNEXT i
Summation follows a similar pattern: set total ← 0, then total ← total + scores[i] inside the loop. To compute the mean, divide the final sum by LEN(scores). Always ensure LEN(scores) > 0 to avoid division by zero.
求和也遵循类似模式:设置 total ← 0,然后在循环内执行 total ← total + scores[i]。要计算平均值,用最终总和除以 LEN(scores)。务必确保 LEN(scores) > 0 以避免除以零。
6. Linear Search | 线性搜索
Linear search scans each element sequentially until the target is found or the end is reached. It works on unsorted data and has a worst-case time complexity of O(n).
线性搜索依次扫描每个元素,直到找到目标或到达数组末尾。它适用于未排序的数据,最坏情况时间复杂度为 O(n)。
A typical pseudocode implementation:
典型的伪代码实现:
position ← -1FOR i ← 0 TO LEN(arr)-1 IF arr[i] = target THEN position ← i BREAK ENDIFNEXT iIF position = -1 THEN OUTPUT 'Not found'ELSE OUTPUT positionENDIF
The BREAK statement exits the loop early once the match is found, improving average efficiency. If position remains -1, the target is absent. This is a building block for many exam questions on algorithm tracing and writing.
一旦找到匹配,BREAK 语句会提前退出循环,从而提高平均效率。如果 position 保持 -1,则目标不存在。这是许多考试中关于算法跟踪和编写题目的基础模块。
7. Binary Search | 二分搜索
Binary search is much faster (O(log n)) but requires the array to be sorted in ascending order. It repeatedly divides the search interval in half by comparing the middle element with the target.
二分搜索要快得多(O(log n)),但要求数组按升序排序。它通过将中间元素与目标比较,反复将搜索区间一分为二。
Pseudocode for binary search on a sorted array list:
在已排序数组 list 上进行二分搜索的伪代码:
low ← 0high ← LEN(list)-1found ← FALSEWHILE low ≤ high AND found = FALSE mid ← (low + high) DIV 2 IF list[mid] = target THEN found ← TRUE ELSE IF list[mid] < target THEN low ← mid + 1 ELSE high ← mid - 1 ENDIFENDWHILEIF found THEN OUTPUT midELSE OUTPUT 'Not found'ENDIF
Note the use of DIV for integer division and careful adjustment of low and high. If the array is not sorted, binary search will not work correctly — a common exam trick.
注意使用 DIV 进行整数除法,并仔细调整 low 和 high。如果数组未排序,二分搜索将无法正确运行——这是考试中常见的陷阱。
8. 2D Arrays | 二维数组
A two-dimensional array extends the concept to rows and columns, like a table or grid. Declaration: DECLARE grid : ARRAY[0:rows-1, 0:cols-1] OF DataType. For a 3×3 grid of integers: DECLARE grid : ARRAY[0:2, 0:2] OF INTEGER.
二维数组将概念扩展到行和列,就像表格或网格。声明:DECLARE grid : ARRAY[0:行数-1, 0:列数-1] OF 数据类型。对于 3×3 的整数网格:DECLARE grid : ARRAY[0:2, 0:2] OF INTEGER。
Access elements with grid[rowIndex, colIndex], e.g., grid[1,0] refers to the first column of the second row. Traversal requires nested loops — the outer loop runs through rows, the inner through columns. Always be consistent about which index represents rows and which columns, as misreading can lead to transposition errors.
使用 grid[行索引, 列索引] 访问元素,例如 grid[1,0] 指第二行第一列。遍历需要嵌套循环——外层循环遍历行,内层遍历列。务必在哪个索引代表行、哪个代表列上保持一致,因为误读会导致转置错误。
2D arrays appear in exam contexts such as storing a chessboard state, a list of students and their subject marks, or an image’s pixel grid. Operations like summing each row or finding the maximum value in a column are typical tasks.
二维数组会出现在考试情境中,例如存储棋盘状态、学生及其科目成绩列表或图像的像素网格。诸如对每一行求和或找出某列的最大值等操作是典型题目。
9. Exam Tips and Common Pitfalls | 考试技巧与常见错误
Always use LEN(arr) to determine size instead of hard‑coding numbers; this prevents off‑by‑one errors and makes your pseudocode adaptable. Check
Published by TutorHao | IGCSE Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply