📚 Arrays in GCSE WJEC Computer Science | GCSE WJEC 计算机:数组 考点精讲
Arrays are one of the fundamental data structures in programming, and the WJEC GCSE Computer Science specification requires a solid understanding of how to declare, initialise, and manipulate them. This guide breaks down the key concepts, common algorithms, and typical exam questions to help you master arrays and achieve a top grade.
数组是编程中最基本的数据结构之一,WJEC GCSE 计算机科学考纲要求考生扎实掌握数组的声明、初始化和操作方法。本指南将逐一拆解关键概念、常见算法和典型考题,帮助你彻底吃透数组知识,在考试中拿到高分。
1. What Is an Array? | 什么是数组?
An array is a collection of elements of the same data type, stored in contiguous memory locations. Each element can be accessed using an index, which usually starts at 0. Arrays allow programmers to store multiple related values under a single identifier, making data handling more efficient and organised.
数组是一组相同数据类型的元素集合,它们在内存中连续存储。每个元素可以通过一个通常从 0 开始的索引来访问。数组让程序员可以用一个标识符存储多个相关联的值,使数据处理更高效、更有条理。
In WJEC pseudocode, arrays are typically declared with a name and a size. For example, ARRAY scores[5] creates a one‑dimensional array that can hold five integer values. The index range is from 0 to 4.
在 WJEC 伪代码中,数组通常用名称和大小来声明。例如,ARRAY scores[5] 创建一个一维数组,可以存储五个整数值,索引范围从 0 到 4。
- Elements are accessed as
scores[0],scores[1], …,scores[4]. - 元素访问形式为
scores[0]、scores[1]……scores[4]。
2. Declaring and Initialising One-Dimensional Arrays | 一维数组的声明与初始化
In the WJEC exam, you must be able to write pseudocode to declare an array and assign initial values. Declaration specifies the array name and size, while initialisation fills the array with data, either by reading user input or by assigning constants.
在 WJEC 考试中,你必须会用伪代码声明数组并赋初值。声明部分确定数组名称和大小,初始化则通过读取用户输入或直接赋值来填充数据。
A typical declaration looks like: ARRAY marks[30]
Then a loop can be used to initialise each element. For instance, to set all elements to zero:
典型的声明形式为:ARRAY marks[30]
然后用一个循环为每个元素赋值。例如,把所有元素设为零:
FOR i ← 0 TO 29
marks[i] ← 0
NEXT i
If you need to read values from the user, the loop would include an INPUT statement:
如果需要从用户读取数据,循环内会包含一个 INPUT 语句:
FOR i ← 0 TO 29
OUTPUT "Enter mark ", i+1
INPUT marks[i]
NEXT i
Be careful: the upper bound is one less than the declared size because of 0‑based indexing. Confusing the size and the last index is a common mistake in exams.
请务必注意:由于索引从 0 开始,循环上界应为声明大小减一。混淆数组长度与最大索引是考试中的常见错误。
3. Reading and Writing Array Elements | 读取与输出数组元素
Once an array is populated, you need to know how to retrieve and display its contents. The same indexing notation is used both for reading from and writing to array positions.
一旦数组填充完毕,你需要知道如何获取和显示其内容。无论是从数组位置读取数据还是向数组位置写入数据,都使用相同的索引表示法。
To output the third element of an array names:
OUTPUT names[2]
输出数组 names 的第三个元素:
OUTPUT names[2]
To traverse the entire array and print all values:
遍历整个数组并打印所有值:
FOR i ← 0 TO LEN(names)-1
OUTPUT names[i]
NEXT i
WJEC pseudocode often uses LEN(arr) to return the number of elements in array arr. Remember that LEN gives the total count, so the last valid index is LEN(arr) - 1.
WJEC 伪代码中常用 LEN(arr) 返回数组 arr 的元素个数。请记住 LEN 返回的是总数,因此最后一个合法索引是 LEN(arr) - 1。
4. Two-Dimensional Arrays | 二维数组
Two-dimensional arrays can be thought of as a table with rows and columns. They are declared with two sizes, representing the number of rows and the number of columns. In WJEC pseudocode, a 2D array might be declared as ARRAY grid[3][4], which creates 3 rows and 4 columns—12 elements in total.
二维数组可以看作一个由行和列组成的表格。声明时需要两个参数,分别代表行数和列数。在 WJEC 伪代码中,二维数组可以声明为 ARRAY grid[3][4],这会生成 3 行 4 列,共 12 个元素。
| Index | Column 0 | Column 1 | Column 2 | Column 3 |
|---|---|---|---|---|
| Row 0 | grid[0][0] | grid[0][1] | grid[0][2] | grid[0][3] |
| Row 1 | grid[1][0] | grid[1][1] | grid[1][2] | grid[1][3] |
| Row 2 | grid[2][0] | grid[2][1] | grid[2][2] | grid[2][3] |
Accessing a 2D array element requires two indices: the row index first, then the column index. To set all elements to zero using nested loops:
访问二维数组元素需要两个索引:先行后列。使用嵌套循环将所有元素设为零:
FOR row ← 0 TO 2
FOR col ← 0 TO 3
grid[row][col] ← 0
NEXT col
NEXT row
Always ensure you match the loop bounds to the declared dimensions. Off-by-one errors are very common here and are frequently tested in tracing exercises.
务必保证循环边界与声明维度相符。这里极易出现差一错误,在追踪类题目中经常考查。
5. Using Arrays to Store and Process Data | 用数组存储和处理数据
Arrays are used when a program must handle collections of similar data, such as a list of student names, daily temperatures, or game scores. Because elements are stored sequentially, we can process them efficiently with loops.
当程序需要处理一组相似数据时(例如学生名单、每日温度、游戏得分),就会用到数组。由于元素按顺序存储,我们可以借助循环高效地进行处理。
Consider a scenario where you need to find the highest score in an array of 50 test results. You would initialise a variable highest to the first element, then loop through the rest, updating highest whenever a larger value is found.
假设你需要找出 50 个测试成绩中的最高分。你可以将变量 highest 初始化为第一个元素,然后遍历剩余元素,每遇到更大的值就更新 highest。
This linear search pattern is a core WJEC algorithm. The pseudocode is:
这种线性搜索模式是 WJEC 的核心算法。伪代码如下:
highest ← scores[0]
FOR i ← 1 TO LEN(scores)-1
IF scores[i] > highest THEN
highest ← scores[i]
ENDIF
NEXT i
OUTPUT highest
Understanding this pattern will not only help with direct array questions but also with problem‑solving tasks that involve searching for maxima, minima, or specific conditions.
掌握这一模式不仅有助于直接回答数组题目,也能帮助解决涉及查找最大值、最小值或满足特定条件的问题。
6. Linear Search with Arrays | 数组中的线性搜索
Linear search is the simplest searching algorithm, used to find whether a particular value exists in an array. It works by checking each element one by one until the target is found or the end of the array is reached.
线性搜索是最简单的查找算法,用于判断数组中是否存在某个特定值。它通过逐个检查每个元素,直到找到目标或到达数组末尾。
The WJEC pseudocode for a linear search that returns the index of a target, or -1 if not found, is:
WJEC 伪代码中,线性搜索返回目标的索引,若未找到则返回 -1:
position ← -1
FOR i ← 0 TO LEN(arr)-1
IF arr[i] = target THEN
position ← i
EXIT FOR
ENDIF
NEXT i
Using an EXIT FOR (or BREAK in some dialects) can make the algorithm more efficient, as it stops once the target is found. However, the exam board may also accept a version that completes the loop but tracks a flag.
使用 EXIT FOR(有些变体中用 BREAK)可以提高算法效率,因为一旦找到目标就会停止。不过考试局同样接受完成整个循环但使用标记变量的写法。
Common exam questions ask you to trace a linear search or to write one from scratch, so being fluent in both the logic and the pseudocode is essential.
常见考题要求你追踪一个线性搜索过程,或者从零开始编写,因此必须熟练掌握其逻辑和伪代码。
7. Binary Search on a Sorted Array | 有序数组中的二分搜索
Binary search is a far more efficient algorithm than linear search, but it requires the array to be sorted. It repeatedly divides the search interval in half by comparing the middle element with the target value.
二分搜索远比线性搜索高效,但它要求数组事先排序。它通过将中间元素与目标值比较,不断将搜索区间对半划分。
In WJEC pseudocode, a binary search can be written as:
用 WJEC 伪代码编写二分搜索如下:
low ← 0
high ← LEN(arr)-1
found ← FALSE
WHILE low ≤ high AND NOT found
mid ← (low + high) DIV 2
IF arr[mid] = target THEN
found ← TRUE
ELSE IF arr[mid] < target THEN
low ← mid + 1
ELSE
high ← mid - 1
ENDIF
ENDWHILE
Note the use of integer division DIV to calculate the middle index. You must be able to trace this algorithm step by step and understand why a sorted array is necessary for it to work correctly.
注意用整数除法 DIV 计算中间索引。你必须能逐步追踪该算法,并理解为什么必须使用有序数组才能保证其正确性。
WJEC papers often feature binary search in the context of a sorted list of numbers or words, such as looking up a pupil name in a sorted register.
WJEC 试卷常把二分搜索放在已排序的数字或单词列表中考,例如在已排序的点名册中查找学生名字。
8. Bubble Sort Using Arrays | 利用数组进行冒泡排序
Sorting is a fundamental operation on arrays, and bubble sort is the simplest sorting algorithm covered in the WJEC specification. It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
排序是数组的基本操作,而冒泡排序是 WJEC 考纲中最简单的排序算法。它会反复遍历列表,比较相邻元素,如果顺序不对就交换它们。
The algorithm stops when no swaps are needed in a complete pass. The pseudocode for bubble sort in ascending order is:
当完成一次遍历而不需要做任何交换时,算法终止。升序排列的冒泡排序伪代码如下:
swap ← TRUE
WHILE swap = TRUE
swap ← FALSE
FOR i ← 0 TO LEN(arr)-2
IF arr[i] > arr[i+1] THEN
temp ← arr[i]
arr[i] ← arr[i+1]
arr[i+1] ← temp
swap ← TRUE
ENDIF
NEXT i
ENDWHILE
Note that the inner loop runs to LEN(arr)-2 because we compare arr[i] with arr[i+1]. Using LEN(arr)-1 would cause an index-out-of-bounds error at the last element.
注意内循环到 LEN(arr)-2,因为我们要比较arr[i] 和 arr[i+1]。若写成 LEN(arr)-1 会在最后一个元素处发生索引越界。
Bubble sort is less efficient for large datasets, but its simplicity makes it a favourite for examining basic algorithm design and trace table questions.
冒泡排序对于大数据集效率较低,但由于其简单,常被用于考查基本算法设计和跟踪表填写。
9. Common Algorithms with Arrays | 数组中的常见算法
WJEC exams often ask you to combine arrays with standard algorithms. You should be comfortable writing pseudocode to:
WJEC 考试常要求你把数组与标准算法结合。你应该能熟练写出伪代码以完成以下任务:
- Find the sum and average of elements.
计算数组元素的总和与平均值。 - Count occurrences of a specific value.
统计某个特定值的出现次数。 - Find the minimum or maximum.
寻找最小值或最大值。 - Concatenate two arrays into a third.
将两个数组合并到第三个数组中。 - Reverse the order of elements.
反转数组元素的顺序。
For example, to count how many times the number 7 appears in an array:
例如,统计数组中数字 7 出现的次数:
count ← 0
FOR i ← 0 TO LEN(arr)-1
IF arr[i] = 7 THEN
count ← count + 1
ENDIF
NEXT i
Another common task is to copy specific elements that meet a condition (e.g. all even numbers) into a new array, which may need an extra counter to track the index in the new array.
另一个常见任务是复制满足特定条件的元素(例如所有偶数)到新数组,这时可能需要额外用一个计数器来跟踪新数组的索引。
Practising these patterns will help you tackle the more complex scenario‑based questions that appear in the component 2 programming paper.
反复练习这些模式有助于应对组件 2 编程卷中更复杂的场景题。
10. Handling Array Bounds and Index Errors | 数组边界与索引错误的处理
One of the most frequent mistakes with arrays is accessing an index outside the valid range. In WJEC pseudocode, arrays are fixed in size and do not automatically resize. Attempting to read or write arr[LEN(arr)] or a negative index is a logic error that can be heavily penalised.
在使用数组时,最常见错误之一就是访问了合法范围外的索引。在 WJEC 伪代码中,数组大小是固定的且不会自动改变。尝试读写 arr[LEN(arr)] 或者负数索引都属于逻辑错误,会被严重扣分。
Always ensure your loop counters stay within 0 to LEN(arr)-1. When writing algorithms like linear search that may require a ‘not found’ sentinel value, use a separate variable (e.g. position ← -1) instead of trying to access an out‑of‑bounds index.
务必确保循环计数器保持在 0 到 LEN(arr)-1 之间。在编写像线性搜索这类可能需要“未找到”标记值的算法时,应使用一个单独变量(如 position ← -1),而不是尝试访问越界索引。
In trace table exercises, you will often be asked to identify the moment when an invalid index is accessed. Accurate understanding of when i reaches the size of the array will help you catch these errors.
在跟踪表练习中,经常要求你识别出访问无效索引的时刻。准确理解 i 何时到达数组长度,有助于你发现这些错误。
11. Parallel Arrays | 平行数组
When you need to store related information of different types that would otherwise require a record structure, parallel arrays are sometimes used. For instance, you might store pupil names in one array and their corresponding scores in a second array, using the same index to link them.
当需要存储不同类型且相互关联的信息,但又无法使用记录结构时,有时会采用平行数组。例如,可以把学生名字存储在一个数组中,把对应的分数存储在另一个数组中,用相同的索引将它们联系起来。
Example: names[0] holds "Alice", and scores[0] holds 85. To find the score of a particular pupil, you might first search names for the target, note the index, and then use that index with scores.
示例:names[0] 存放 "Alice",scores[0] 存放 85。要查找某个学生的分数,可以先在 names 数组中搜索目标,记下索引,再将该索引用于 scores 数组。
While parallel arrays are accepted in WJEC, they should be used with care. Maintaining consistency between the arrays is the programmer's responsibility, and errors can easily be introduced if an element is added to one array but not the other.
WJEC 允许使用平行数组,但必须谨慎。程序员需要负责保持数组之间的一致性,如果在一个数组中添加了元素而在另一个中没有添加,就很容易引入错误。
12. Arrays in Exam Questions – Tips and Tricks | 考题中的数组 – 技巧与提示
When tackling array questions in the WJEC exam, keep the following points in mind:
在 WJEC 考试中处理数组题目时,请牢记以下几点:
- Read the question carefully to determine whether the array indices start at 0 or 1 – WJEC pseudocode mainly uses 0‑based indexing, but some legacy questions may use 1‑based. Always check the context.
仔细读题,确认数组索引从 0 还是 1 开始——WJEC 伪代码主要使用从 0 开始的索引,但一些旧题可能从 1 开始。务必根据上下文判断。 - Use consistent notation:
ARRAY name[size]for declaration, andname[i]for element access.
使用一致的表示法:ARRAY name[size]用于声明,name[i]用于元素访问。 - Never try to use dynamic arrays that change size – WJEC expects you to declare a fixed size that is large enough for the problem.
不要使用会改变大小的动态数组——WJEC 希望你声明一个大小固定且足够解决问题的数组。 - Draw a quick trace table in your answer space for complex loops and conditionals – it will help you avoid off‑by‑one errors.
对于复杂的循环和条件判断,在答题位置快速画一个跟踪表——这能帮助你避免差一错误。 - Remember that string handling in pseudocode often treats a string as an array of characters, so array concepts also apply when reversing a string or counting vowels.
记住伪代码中的字符串处理常把字符串看作字符数组,因此反转字符串或统计元音字母时,数组概念同样适用。
Mastering arrays gives you a strong foundation not only for the GCSE but also for A‑level and beyond, as they underpin nearly every other data structure.
掌握数组不仅为 GCSE 打下坚实基础,对 A‑level 乃至更高阶段的学习也至关重要,因为几乎所有其他数据结构都以数组为基础。
Published by TutorHao | GCSE WJEC 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