📚 GCSE OCR Computer Science: Arrays – Key Exam Points | GCSE OCR 计算机:数组 考点精讲
In the OCR GCSE Computer Science specification, arrays are one of the most important data structures you must master. Questions on arrays appear in both Paper 1 (Computer Systems) and Paper 2 (Computational Thinking, Algorithms and Programming), often embedded in algorithm design, trace tables, or pseudocode tasks. Understanding how to declare, initialise, access, and manipulate arrays is essential for achieving top grades. This article will break down all key exam points surrounding arrays, using clear examples in OCR Exam Reference Language and offering plenty of practice tips.
在 OCR GCSE 计算机科学考试大纲中,数组是必须掌握的最重要的数据结构之一。与数组相关的题目既出现在试卷一(计算机系统),也出现在试卷二(计算思维、算法与编程)中,通常嵌入在算法设计、跟踪表或伪代码任务里。理解如何声明、初始化、访问和操作数组对于取得高分至关重要。本文将围绕数组的所有关键考点,使用清晰的 OCR 考试参考语言示例,并提供充分的练习建议。
1. What is an Array? | 什么是数组?
An array is a collection of elements, all of the same data type, stored in contiguous memory locations. Each element can be directly accessed using an index (position). Unlike simple variables that can hold only one value at a time, an array can store many values under a single identifier, making it ideal for handling lists of data, such as test scores, temperatures, or names. In OCR’s context, arrays are static data structures – once created, their size usually cannot change during program execution, so you must plan ahead.
数组是相同数据类型、存储在连续内存空间中的元素集合。每个元素都可以通过索引(位置)直接访问。与一次只能存放一个值的简单变量不同,数组可以在一个标识符下存储多个值,因此非常适合处理数据列表,例如考试成绩、温度或名字。在 OCR 的语境中,数组属于静态数据结构——一旦创建,其大小在程序运行过程中通常无法更改,因此你必须提前规划。
- Arrays store multiple values of the same data type. / 数组存储相同数据类型的多个值。
- They occupy a continuous block of memory. / 它们占用连续的内存块。
- Elements are accessed via an index, typically starting at 0 in the OCR Exam Reference Language. / 元素通过索引访问,OCR 考试参考语言中的索引通常从 0 开始。
2. Declaring and Initialising Arrays | 声明和初始化数组
In the OCR Exam Reference Language (ERL), you can declare an array by specifying the data type, an identifier, and the size in square brackets. You can also initialise an array with values directly. Always remember to choose a meaningful name for your array – just like you would for any variable. Below are the common declaration styles you will see in exam questions.
在 OCR 考试参考语言(ERL)中,你可以通过指定数据类型、标识符以及方括号中的大小来声明数组。你也可以直接使用值来初始化数组。永远记住,要为数组选择一个有意义的名称——就像你在命名普通变量时一样。下面是你将在考题中遇到的常见声明形式。
| Declaration Style | Example | Explanation |
|---|---|---|
| Fixed size | array scores[5] |
Creates an array of 5 elements, all initially empty/default. |
| With values | array ages = [16, 17, 18] |
Creates an array of 3 integers and fills them immediately. |
| String array | array pupils[30] of string |
Declares an array that can hold 30 strings. |
中文对照:固定大小声明 array scores[5] 创建一个具有 5 个元素的数组,初始为默认值。用值初始化 array ages = [16, 17, 18] 立即创建含 3 个整数的数组。字符串数组 array pupils[30] of string 声明一个可容纳 30 个字符串的数组。务必注意,在 ERL 中声明多维数组(如二维数组)的写法是 array grid[3,4],我们会在后面专门讲解。
3. Indexing and Accessing Elements | 索引和访问元素
To read or modify a single element, you use the array name followed by the index in square brackets. In the OCR ERL, indexing is zero-based: the first element is at index 0, the second at index 1, and so on. For an array declared as array scores[5], valid indices range from 0 to 4. Trying to access an index outside this range will cause a runtime error – a classic exam trap. You can assign a value to an individual element using the assignment operator.
要读取或修改单个元素,你需要使用数组名加上方括号中的索引。在 OCR ERL 中,索引从零开始:第一个元素位于索引 0,第二个元素位于索引 1,以此类推。对于声明为 array scores[5] 的数组,有效索引范围是 0 到 4。试图访问范围之外的索引将导致运行时错误——这是经典的考试陷阱。你可以使用赋值运算符为单个元素赋值。
scores[0] = 12
output scores[2]
If you output scores[5] on an array of length 5, you will get an ‘index out of bounds’ error. Be very careful with loop counters that might go one step too far.
如果你在长度为 5 的数组上输出 scores[5],就会引发“索引越界”错误。使用循环计数器时务必小心,避免多走一步导致越界。
4. Traversing Arrays with Loops | 使用循环遍历数组
Traversal means visiting every element of an array, usually to read, process, or update the values. The two most common looping structures in OCR pseudocode are for loops using an index variable and for each loops that iterate directly over elements. Iteration is a core skill, and you may be asked to write, complete, or trace a loop that passes through an array.
遍历的意思是访问数组中的每一个元素,通常是进行读取、处理或更新数值。OCR 伪代码中最常见的两种循环结构是使用索引变量的 for 循环和直接迭代元素的 for each 循环。循环迭代是一项核心技能,考试中可能需要你写出、补全或跟踪一个遍历数组的循环。
Index-based iteration:
for i = 0 to ages.length - 1
output ages[i]
next i
使用索引迭代:循环变量 i 从 0 到数组长度减 1,依次输出每个元素。对于 OCR ERL 数组,.length 属性返回元素的总个数,因此最后一个有效索引总是 length - 1。
Element-based iteration (for each):
for each age in ages
output age
next age
基于元素的迭代(for each):变量 age 直接获取 ages 数组中的每个值,避免了手动管理索引,代码更简洁,但无法直接修改原数组的值。
5. Common Array Algorithms: Searching | 常见数组算法:搜索
Two searching algorithms are explicitly taught in OCR GCSE: linear search and binary search. Linear search works on unsorted arrays by checking each element in order until a match is found or the end is reached. Binary search requires a sorted array and repeatedly halves the search interval, which makes it much faster on large data sets. You need to be able to write both algorithms in pseudocode, trace their steps, and explain their efficiency.
OCR GCSE 明确教授两种搜索算法:线性搜索和二分搜索。线性搜索可以在未排序的数组上工作,它按顺序检查每个元素,直到找到匹配项或到达末尾。二分搜索要求数组已排序,并通过反复将搜索区间减半来快速定位,这使得它在大型数据集上快得多。你需要能够用伪代码编写这两种算法,逐步跟踪其执行过程,并解释它们的效率。
Linear search pattern:
found = false
index = 0
while found == false and index < items.length
if items[index] == target then
found = true
output index
else
index = index + 1
endif
endwhile
线性搜索模式:用布尔变量 found 标记是否找到,循环直到找到或索引越界。如果未找到,通常输出一个特殊值如 -1。
Binary search key points (requires sorted array):
- Set low to 0 and high to length – 1. / 将 low 设为 0,high 设为 length – 1。
- While low ≤ high, calculate mid = (low + high) DIV 2 (integer division). / 当 low ≤ high 时,计算 mid = (low + high) 整除 2。
- If array[mid] equals target, search succeeds. / 如果 array[mid] 等于目标值,则搜索成功。
- If target is smaller, set high = mid – 1; if larger, set low = mid + 1. / 若目标值更小,则设 high = mid – 1;若更大,则设 low = mid + 1。
Both algorithms can be tested with trace tables – always show the values of low, high, mid and the condition at each step.
两种算法都可能通过跟踪表考查——你必须在每一步中显示 low、high、mid 以及判断条件的值。
6. Common Array Algorithms: Finding Min, Max and Total | 常见数组算法:求最小、最大和总和
These standard algorithms appear frequently in both theory and programming tasks. Finding the minimum or maximum value involves initialising a variable with the first element and then comparing every other element against it. Computing the total (sum) uses an accumulator variable. You are also expected to calculate the average by dividing the total by the number of elements. The ability to adapt these patterns to two-dimensional arrays or to partial ranges shows deeper understanding.
这些标准算法经常出现在理论和编程任务中。求最小值或最大值需要先用第一个元素初始化一个变量,然后将其他每个元素与之比较。计算总和则使用一个累加器变量。你还需要用总和除以元素个数来计算平均值。能够将这些模式应用到二维数组或部分区间上,能体现出对知识的深入理解。
Finding maximum:
max = scores[0]
for i = 1 to scores.length - 1
if scores[i] > max then
max = scores[i]
endif
next i
求最大值:假设第一个元素最大,然后从索引 1 开始遍历,每遇到更大的值就更新 max。最小值的代码类似,只需将 > 改为 <。
Total and average:
total = 0
for each s in scores
total = total + s
next s
average = total / scores.length
总和与平均值:累加每个元素到 total,最后除以数组长度。在伪代码中通常使用 / 表示除法,但如果目标是整数运算,可能会涉及实数或取整说明。
7. Two-Dimensional Arrays | 二维数组
A two-dimensional (2D) array can be thought of as a table with rows and columns. In OCR ERL, you declare a 2D array by providing two size values, e.g., array grid[3,4] creates 3 rows and 4 columns. Access an element using grid[row, col], remembering that both indices start at 0. 2D arrays are extremely useful for representing boards, spreadsheets, images, or seating plans.
二维数组可以看作带有行和列的表格。在 OCR ERL 中,声明二维数组时提供两个大小值,例如 array grid[3,4] 创建 3 行 4 列。使用 grid[row, col] 访问元素,请记住两个索引都从 0 开始。二维数组在表示棋盘、电子表格、图像或座位表时非常有用。
Nested loops are needed to fully traverse a 2D array:
for row = 0 to 2
for col = 0 to 3
output grid[row, col]
next col
next row
遍历二维数组需要使用嵌套循环:外层循环控制行,内层循环控制列。你也可以用 grid.length 获取行数(第一维长度),而列的尺寸则假设每行长度相同;在 ERL 中有时可以写 grid[0].length,但最常见的考试写法是直接给出确定的数字。
8. Arrays and Subprograms (Functions/Procedures) | 数组与子程序(函数/过程)
Arrays can be passed as parameters to procedures and functions, enabling you to write reusable code. When an array is passed, the subprogram can read or modify its elements, but you must be careful because any changes made to the array inside the subprogram will affect the original array (arrays are passed by reference in OCR’s model). Exam questions may ask you to write a function that returns an average, or a procedure that sorts an array.
数组可以作为参数传递给过程和函数,帮助你写出可复用的代码。传递数组时,子程序可以读取或修改其元素,但你必须小心,因为子程序内部对数组所做的任何更改都会影响原始数组(在 OCR 模型中,数组按引用传递)。考题可能要求你编写一个返回平均值的函数,或者一个对数组排序的过程。
Example function that returns the total:
function sumArray(arr[])
total = 0
for each x in arr
total = total + x
next x
return total
endfunction
示例:函数 sumArray 接收一个数组 arr,计算总和并返回。调用时使用 result = sumArray(scores)。注意形参中 arr[] 表明接受了数组参数。
9. Common Errors and Debugging Tips | 常见错误和调试技巧
Students often lose marks because of off-by-one errors, uninitialised array indices, confusing zero-based and one-based indexing, or assuming arrays are dynamic when they are static. Other typical mistakes include forgetting to reset an accumulator when reusing an array, attempting to access an element with a negative index, and writing infinite loops because a boolean flag is never updated. Being able to spot these errors in a given pseudocode snippet is a key exam skill.
学生常因差一错误、未被初始化的数组索引、混淆从 0 开始的索引与从 1 开始的索引,或者在数组为静态时认为它们是动态的而失分。其他典型错误还包括:重复使用数组时忘记重置累加器、尝试用负索引访问元素,以及因为布尔标记从未更新而写出死循环。能够从提供的伪代码片段中发现这些错误是一项关键的应试技能。
- Off-by-one:
for i = 0 to scores.lengthiterates one time too many. / 差一错误:for i = 0 to scores.length多迭代了一次。 - Uninitialised element: reading
scores[2]before it has been given a value may produce unexpected results. / 未初始化元素:在给scores[2]赋值之前读取它,可能会产生意外结果。 - Negative index:
scores[-1]is invalid and will cause an error. / 负索引:scores[-1]是无效的并将引发错误。 - Missing reset: reusing an array without resetting a counter variable may carry over old values. / 缺少重置:重复使用数组但未重置计数器变量,可能会保留旧值。
10. Arrays in OCR Exam Reference Language | OCR考试参考语言中的数组
You must be thoroughly familiar with the ERL syntax because exam board materials and questions use this notation. The key syntax points: array declaration uses the word array; 1D arrays have a single size in brackets, 2D arrays have two sizes separated by a comma; assignment to an element uses =; the .length attribute gives the number of elements. Arrays are always of a uniform data type, and the notation of string or of integer can clarify the type when needed. All indices are integers and start at 0.
你必须完全熟悉 ERL 语法,因为考试局材料和试题都采用这种表示法。关键语法点:数组声明使用单词 array;一维数组在括号中有一个大小,二维数组有两个大小并以逗号分隔;对元素赋值使用 =;.length 属性返回元素个数。数组总是具有统一的数据类型,必要时可使用 of string 或 of integer 来标明类型。所有索引都是整数并从 0 开始。
Quick reference table:
| Operation | ERL Example |
|---|---|
| Declare 1D | array names[20] |
| Declare 2D | array board[8,8] |
| Assign element | names[0] = "Alice" |
| Access element | print(board[3,2]) |
| Get length | len = names.length |
上表概括了最常用的 ERL 操作。在考试中,建议你在写算法时多使用注释,并清晰地标明索引范围,这能帮助阅卷老师理解你的思路。
11. Practice Exam-Style Questions | 模拟考题演练
Let’s apply your knowledge with a typical OCR-style task. Imagine you are given the following array: array temps = [18, 21, 19, 23, 20]. (a) Write an algorithm to output all temperatures above 20. (b) Write a function that returns the highest temperature. (c) A 2D array array readings[3,4] stores hourly temperatures for 3 days and 4 time slots; write nested loops to calculate the weekly total. Working through such questions will solidify your understanding and exam technique.
让我们通过典型的 OCR 风格任务来应用所学知识。假设你得到以下数组:array temps = [18, 21, 19, 23, 20]。(a) 写出一个算法,输出所有高于 20 的温度。(b) 编写一个返回最高温度的函数。(c) 二维数组 array readings[3,4] 存储了 3 天、每天 4 个时段的温度数据;写出嵌套循环来计算总分。仔细完成这类问题将巩固你的理解和应考技巧。
Sample solution for (a):
for each t in temps
if t > 20 then
output t
endif
next t
对于 (b),可以使用第 6 节提到的最大值算法;(c) 需要嵌套循环累加所有 readings[day][slot],在课程中练习时请写全变量声明。
12. Summary and Key Revision Points | 总结与核心复习要点
Arrays are a cornerstone of GCSE Computer Science – they underpin data handling, searching, sorting, and much of algorithmic thinking. Remember the following key revision points: arrays are indexed from 0; the last index is always length - 1; loops are used to traverse arrays; linear search does not require sorted data, binary search does; 2D arrays need nested loops; and subprograms can both receive and modify arrays. Keep practising pseudocode with OCR ERL syntax, and use trace tables to check your algorithms step by step. With a clear understanding of arrays, you will be well prepared for the exam.
数组是 GCSE 计算机科学的基石——它们支撑着数据处理、搜索、排序以及大部分算法思维。请牢记以下核心复习要点:数组索引从 0 开始;最后一个索引总是 length - 1;使用循环遍历数组;线性搜索不需要排序,二分搜索需要;二维数组需要嵌套循环;子程序可以接收并修改数组。持续用 OCR ERL 语法练习伪代码,并使用跟踪表逐步检查你的算法。清晰地理解数组,你将能自信应对考试。
Published by TutorHao | GCSE Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导