Writing and Amending Algorithms | 编写和修改算法

📚 Writing and Amending Algorithms | 编写和修改算法

In the Cambridge IGCSE Computer Science syllabus, writing and amending algorithms is one of the most important skills you will be tested on. An algorithm is a precise, step-by-step set of instructions used to solve a problem, and examiners expect you to design clear algorithms, follow them using trace tables, and modify them correctly when requirements change.

在剑桥 IGCSE 计算机科学课程中,编写和修改算法是你最需要掌握的核心技能之一。算法是解决问题的一组精确、逐步执行的指令;考官期望你能够设计出清晰的算法,使用跟踪表逐行检查算法,并在需求变化时正确修改算法。


1. What Is an Algorithm | 什么是算法

An algorithm is a finite sequence of well-defined, unambiguous steps that, when followed correctly, transforms an input into an output and solves a specific problem. In computer science, algorithms can be expressed in three main ways: pseudocode, flowcharts, and program code written in a high-level language.

算法是有限的一系列定义清晰、没有歧义的步骤;按照这些步骤正确执行,就能把输入转换为输出,从而解决具体问题。在计算机科学中,算法通常用三种方式表示:伪代码、流程图,以及用高级语言编写的程序代码。

For an algorithm to be considered valid, it must have three key characteristics: it must terminate after a finite number of steps, each instruction must be clear and unambiguous, and it must be general enough to handle a whole class of similar problems rather than just one single case.

一个算法要想有效,必须具备三个关键特征:必须在有限步骤后终止;每一条指令都必须清晰且没有歧义;必须具有一定的通用性,能够处理一类相似问题,而不仅仅针对某一个具体例子。

For example, the steps “pick up the phone, unlock it, open the calculator app, enter 2 + 3, press equals” form an algorithm for adding two numbers on a phone. In the IGCSE exam, you will normally be asked to write algorithms using either pseudocode or flowchart symbols, so you must be fluent in both.

例如,“拿起手机、解锁、打开计算器应用、输入 2 + 3、按等号”这一系列步骤就是手机上计算两个数字之和的算法。在 IGCSE 考试中,你通常需要用伪代码或流程图符号来写算法,因此你必须熟练掌握这两种表示方式。


2. The Three Basic Constructs | 三种基本结构

A well-written algorithm is built from just three basic constructs: sequence, selection, and iteration. All structured algorithms, no matter how complex, are combinations of these three building blocks. Examiners will check that your algorithms use these constructs correctly.

一个编写良好的算法只由三种基本结构组成:顺序、选择和循环。无论多复杂的算法,本质上都是这三种基本结构的组合。考官会检查你编写的算法是否正确使用了这些结构。

  • Sequence | 顺序:

    Instructions are executed one after another in the order they appear. For example, entering your name, then entering your age, then displaying a greeting.

    指令按照其出现的顺序逐条执行。例如:先输入姓名,再输入年龄,最后显示问候语。

  • Selection | 选择:

    The algorithm chooses between two or more alternative paths depending on a condition. Selection is implemented with IF statements or CASE statements.

    算法根据条件在两条或多条可选路径中选择一条执行。选择结构通过 IF 语句或 CASE 语句实现。

  • Iteration | 循环:

    A block of statements is repeated either a fixed number of times (count-controlled using a FOR loop) or until a condition is met (condition-controlled using WHILE or REPEAT loops).

    一组语句被重复执行:要么重复固定次数(使用 FOR 循环,即计数控制循环),要么一直重复到某个条件成立为止(使用 WHILE 或 REPEAT 循环,即条件控制循环)。

You should be able to spot which construct is being used in any algorithm you read, and you should deliberately choose the correct construct when writing your own. A common exam mistake is using a FOR loop when the number of repetitions is unknown — in that case a WHILE loop is more appropriate.

你应该能从任何给定的算法中辨认出使用了哪种结构,并且在编写自己的算法时有意识地选择合适的结构。考试中常见的错误是:在重复次数未知的情况下使用 FOR 循环,而这时使用 WHILE 循环才更合适。


3. Writing Pseudocode | 编写伪代码

Pseudocode is a simple, human-readable way of writing an algorithm without worrying about the exact syntax of a programming language. Cambridge IGCSE uses its own standard pseudocode conventions, and using them correctly will earn you marks even if you do not know a real programming language.

伪代码是一种简单、便于人类阅读的算法描述方式,不需要关心某种编程语言的具体语法。剑桥 IGCSE 使用自己规定的标准伪代码约定;即使你不会任何一种真实编程语言,只要按照这些约定书写,就能得分。

The table below summarises the key pseudocode constructs you must know for the exam.

下表总结了你在考试中必须掌握的关键伪代码结构。

Construct | 结构 Pseudocode Example | 伪代码示例
Input | 输入 INPUT Number
Output | 输出 OUTPUT “Hello”
Assignment | 赋值 Total ← 0
Selection | 选择 IF x > 5 THEN … ELSE … ENDIF
Count-controlled loop | 计数循环 FOR count ← 1 TO 10 … NEXT count
Condition-controlled loop | 条件循环 WHILE x > 0 … ENDWHILE
Repeat until | 重复直到 REPEAT … UNTIL x = 0

Notice that assignment uses the left arrow symbol, written as ““. When you read the line Total ← Total + 1, it means “take the current value of Total, add 1 to it, and store the result back into Total”. This is different from a mathematical equation, and examiners expect you to understand this distinction.

请注意,赋值使用左箭头符号 ““。当你读到 Total ← Total + 1 这一行时,意思是“取出 Total 当前的值,加 1,再把结果存回 Total”。这不同于数学中的等式,考官期望你理解这一区别。

When writing an IF statement, you must remember to close it with ENDIF. For multi-way selection, you can use CASE: CASE Age OF 1: … 2: … OTHERWISE: … ENDCASE. Always keep the keywords in uppercase and make sure every structure that opens also closes.

编写 IF 语句时,必须记得用 ENDIF 结束。对于多分支选择,可以使用 CASE 语句:CASE Age OF 1: … 2: … OTHERWISE: … ENDCASE。关键字一律大写,并且确保每个开始的结构都有对应的结束。


4. Flowchart Symbols | 流程图符号

A flowchart is a graphical way of representing an algorithm. Each type of step is drawn with a different shape, and arrows connect the shapes to show the order in which steps are carried out. In the exam, you may be asked to draw a flowchart or to interpret one that has been given to you.

流程图是用图形表示算法的方式。每一步都用不同的形状绘制,箭头连接各个形状,表示步骤执行的先后顺序。在考试中,你可能会被要求画出流程图,或者解释一个已经给出的流程图。

Symbol | 符号 Shape | 形状 Meaning | 含义
Start / Stop | 开始 / 结束 Rounded rectangle | 圆角矩形 Marks the beginning or end of the algorithm
Input / Output | 输入 / 输出 Parallelogram | 平行四边形 Represents reading data or displaying results
Process | 处理 Rectangle | 矩形 A calculation or assignment such as Total ← Total + 1
Decision | 判断 Diamond | 菱形 A yes/no question that chooses one of two branches
Flow line | 流向线 Arrow | 箭头 Shows the direction of flow between steps

A decision symbol must always have exactly one entry point and two exit paths, usually labelled “Yes” and “No”. When drawing a flowchart, ensure the flow lines do not cross if possible, and label decisions clearly with conditions such as Number > 0?. Keep your flowchart neat because examiners reward clearly readable diagrams.

判断符号必须有且只有一个入口和两条出口路径,通常标为”Yes”和”No”。画流程图时,尽量让流向线不交叉,并且用清晰的条件标注判断,例如 Number > 0?。保持流程图整洁,因为考官会奖励清晰可读的图表。


5. Writing an Algorithm Step by Step | 一步一步编写算法

When you are asked to write an algorithm, do not panic. A systematic approach will help you produce a correct and complete answer. First, read the problem statement carefully and identify exactly what the inputs, outputs, and processing requirements are.

当你被要求编写算法时,不要慌张。按系统化的步骤进行,就能写出正确完整的答案。首先仔细阅读题目,明确输入是什么、输出是什么、需要处理哪些数据。

Second, decompose the problem into smaller parts. For example, a program that finds the average of ten test scores can be broken into three tasks: read the scores, add them together, and divide by ten. Solving each sub-task separately makes the whole algorithm easier to write.

第二,把问题分解成若干较小的部分。例如,计算十个考试分数平均值的程序可以分解为三个任务:读取分数、把所有分数相加、再除以十。分别解决每个子任务,会让整个算法更容易编写。

Third, choose the right constructs. Use a FOR loop when the number of repetitions is known in advance, a WHILE loop when the algorithm must keep going until a condition becomes false, and a REPEAT…UNTIL loop when the body must run at least once. Use IF or CASE whenever a decision must be made.

第三,选择正确的结构。当重复次数预先已知时使用 FOR 循环;当算法必须一直执行直到某个条件变为假时使用 WHILE 循环;当循环体至少要执行一次时使用 REPEAT…UNTIL 循环;当需要做决定时使用 IF 或 CASE 语句。

Finally, present your algorithm clearly. In an exam, write your pseudocode starting on a fresh page, indent each nested structure to show which block belongs to which statement, and use meaningful variable names such as Total, Count, and Highest rather than single letters like X and Y.

最后,清楚地呈现算法。考试时,从新的一页开始写伪代码,用缩进表示嵌套结构之间的所属关系,并为变量取有意义的名字,例如 Total、Count、Highest,而不是用 X、Y 等单个字母。


6. Trace Tables | 跟踪表

A trace table is a table used to record the state of all variables and outputs each time a statement in the algorithm is executed. By completing a trace table, you can follow the logic of an algorithm step by step and check whether it produces the expected output. Trace tables are also a powerful tool for finding logic errors.

跟踪表是一种表格,用于在算法每次执行语句时记录所有变量和输出的状态。通过填写跟踪表,你可以逐行跟踪算法的逻辑,检查它是否产生预期输出。跟踪表也是查找逻辑错误的强大工具。

Every column of a trace table is headed with the name of a variable, with one extra column for output. Below is an example trace table for this simple algorithm, which finds the largest number from a list by comparing each new value with the current maximum.

跟踪表的每一列都以变量名作为表头,另外附加一列表示输出。下面是一个简单算法的跟踪表示例,该算法通过将每个新值与当前最大值进行比较,找出一组数中的最大数。

INPUT N
largest ← 0
FOR count ← 1 TO N
    INPUT number
    IF number > largest THEN
        largest ← number
    ENDIF
NEXT count
OUTPUT largest

Suppose the data entered is N = 4 and the numbers are 5, 3, 8, 2. The trace table would develop as follows, where we read across one row each time the loop body is executed.

假设输入 N = 4,数字依次是 5、3、8、2。跟踪表会像下面这样逐步填充,每次执行循环体时读取一行。

count | 变量 count number | 变量 number largest | 变量 largest Output | 输出
1 5 5
2 3 5
3 8 8
4 2 8 8

Notice that when the condition number > largestPublished by TutorHao | IGCSE 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