📚 Writing and Amending Algorithms | 编写与修正算法
Algorithms are the foundation of computational thinking in science. Whether you are planning an experiment, processing data, or modelling a system, a clear step-by-step procedure ensures reliable results. This article covers how to write, represent, and amend algorithms in a scientific context, focusing on the skills needed for IGCSE-level science and computing tasks.
算法是科学领域中计算思维的基础。无论你是在规划实验、处理数据还是建立系统模型,一个清晰的逐步过程都能确保结果的可靠性。本文将介绍如何在科学情境下撰写、表示和修正算法,重点培养IGCSE科学和计算任务所需的技能。
1. Understanding Algorithms | 理解算法
An algorithm is a precise sequence of instructions designed to solve a specific problem or carry out a task. In everyday life, a cooking recipe acts as an algorithm; in science, an algorithm could describe how to calculate the density of an object. Every algorithm must have a definite start, well-defined steps, and a clear end point.
算法是为解决特定问题或执行任务而设计的精确指令序列。在日常生活中,一份烹饪食谱就可以看作是一种算法;在科学中,算法可以描述如何计算物体的密度。每个算法都必须有明确的开始、定义清晰的步骤以及清楚的结束点。
Good algorithms share three key properties: they are unambiguous (each step has only one meaning), they are finite (they complete within a limited number of steps), and they are effective (they produce the correct output when followed correctly).
好的算法具备三个关键特性:明确性(每一步只有一种含义)、有限性(在有限步骤内完成)和有效性(正确遵循时能产生正确的输出)。
2. Algorithms in Science | 科学中的算法
In science, algorithms are not just for computer programs. They are vital for designing fair experiments, recording observations, and analysing data. For instance, an algorithm for measuring the rate of a reaction might detail the sequence: collect gas in a syringe every 10 seconds for 2 minutes and then calculate the average rate.
在科学中,算法不仅仅用于计算机程序。它们对于设计公平的实验、记录观察结果和分析数据至关重要。例如,用于测量反应速率的算法,可以详细描述以下序列:每10秒用注射器收集气体,持续2分钟,然后计算平均速率。
Data-processing algorithms are especially common. To find the mean of ten mass readings, an algorithm explains: ‘Add all ten values, then divide the total by 10.’ When writing and amending such algorithms, the scientist must think about possible exceptions, such as zero readings or missing data.
数据处理算法尤其常见。要计算十个质量读数的平均值,算法会说明:“将所有十个值相加,然后用总和除以10。”在编写和修正这样的算法时,科学家必须考虑可能出现的异常情况,比如零读数或缺失数据。
3. Writing Algorithms – Pseudocode | 编写算法——伪代码
Pseudocode is a simplified, human-readable notation that outlines an algorithm without being tied to a specific programming language. It uses plain English and common programming structures. For IGCSE science problems, pseudocode helps communicate logic clearly before implementation.
伪代码是一种简化的、人类可读的符号,用于勾勒算法,而不受特定编程语言的限制。它使用平实的英文和常见的编程结构。对于IGCSE科学问题,伪代码有助于在实施之前清晰地传达逻辑。
A pseudocode algorithm to compute the density of a liquid might look like this:
计算液体密度的伪代码算法可能如下所示:
INPUT mass, volume
IF volume = 0 THEN
OUTPUT “Error: division by zero”
ELSE
density ← mass / volume
OUTPUT density
ENDIF
Notice the use of INPUT, OUTPUT, IF, ELSE, and the assignment arrow ←. This structure ensures anyone reading the pseudocode can follow the logic.
注意其中INPUT、OUTPUT、IF、ELSE以及赋值箭头←的使用。这种结构确保任何阅读伪代码的人都能理解其中的逻辑。
4. Flowcharts as Visual Algorithms | 流程图作为可视化算法
Flowcharts represent algorithms graphically, using standard symbols connected by arrows. They are especially useful in science when you need to visualise decision-making processes, such as whether a measurement falls within an acceptable range.
流程图用标准符号和箭头以图形方式表示算法。当需要将决策过程可视化时(例如判断测量值是否落在可接受范围内),流程图在科学中尤其有用。
- Oval: Start and End points
- Rectangle: A process or action, e.g. ‘Measure 25 cm³ of acid’
- Parallelogram: Input or output, e.g. ‘Enter the starting temperature’
- Diamond: A decision, e.g. ‘Is the indicator colour pink?’
- Arrow: Shows the direction of flow
- 椭圆形:开始和结束
- 矩形:过程或动作,例如“量取25 cm³的酸”
- 平行四边形:输入或输出,例如“输入起始温度”
- 菱形:判断,例如“指示剂颜色是粉红色吗?”
- 箭头:表示流程方向
When drawing a flowchart, keep it neat and test it by tracing the arrows with sample data. A well-structured flowchart makes it easier to spot logical errors before writing any code.
绘制流程图时,要保持整洁,并通过示例数据追踪箭头来进行测试。一个结构良好的流程图能让你在编写任何代码之前更容易发现逻辑错误。
5. Variables and Assignments | 变量与赋值
In an algorithm, a variable is a named container that stores a value which can change during execution. In a science investigation, you might use variables such as initialTemperature, finalTemperature, and temperatureChange.
在算法中,变量是一个有名称的容器,用于存储在执行过程中可以改变的值。在科学探究中,你可能会用到诸如initialTemperature(初始温度)、finalTemperature(最终温度)和temperatureChange(温度变化)等变量。
Assigning a value to a variable is shown with an arrow or equals sign. For example:
temperatureChange ← finalTemperature − initialTemperature
This statement calculates the difference and puts it into the variable on the left. Always ensure variables are given meaningful names so that your algorithm remains readable.
为变量赋值用箭头或等号表示。例如:
temperatureChange ← finalTemperature − initialTemperature
该语句计算出差值,并将其放入左侧的变量中。务必确保变量命名有意义,这样你的算法才能保持可读性。
6. Conditions and Selection | 条件与选择结构
Selection allows an algorithm to make decisions based on the data it encounters. The classic ‘IF…THEN…ELSE’ structure is used whenever a choice must be made. In a science algorithm, you might check whether a result is anomalous.
选择结构允许算法根据遇到的数据做出决策。只要需要做出选择,就会用到经典的“IF…THEN…ELSE”结构。在科学算法中,你可能会检查某个结果是否为异常值。
Example: An algorithm that instructs a student to repeat a titration if two titres differ by more than 0.2 cm³.
示例:一个算法指令学生,如果两次滴定体积相差超过0.2 cm³,则重复滴定。
IF |titre1 − titre2| > 0.2 THEN
OUTPUT “Repeat titration”
ELSE
meanTitre ← (titre1 + titre2) / 2
OUTPUT meanTitre
ENDIF
This use of a condition prevents poor-quality data from being used in further calculations and is a key aspect of amending algorithms for better accuracy.
这种条件的使用可以防止质量不佳的数据进入后续计算,这正是修正算法以提高准确性的一个关键方面。
7. Loops and Repetition | 循环结构
Repetition structures (loops) allow a set of instructions to be executed multiple times. In science, loops are used whenever you repeat a measurement or iterate through a data set. Two common types are FOR loops and WHILE loops.
循环结构允许多次执行一组指令。在科学中,每当你重复测量或迭代数据集时,就会用到循环。两种常见类型是FOR循环和WHILE循环。
A FOR loop runs a known number of times:
FOR count FROM 1 TO 5 DO
Take a temperature reading
ENDFOR
A WHILE loop continues as long as a condition is true, such as WHILE the temperature is below 80 °C, keep stirring. It is essential to ensure the condition eventually becomes false; otherwise, you create an infinite loop, a common error when writing algorithms.
FOR循环运行已知的次数:
FOR count FROM 1 TO 5 DO
记录一次温度读数
ENDFOR
WHILE循环在条件为真时持续运行,例如当温度低于80 °C时要持续搅拌。关键是要确保条件最终会变为假;否则就会产生无限循环,这是编写算法时常见的错误。
8. Common Algorithm Errors | 常见算法错误
When writing and amending algorithms, students frequently encounter three main error types:
在编写和修正算法时,学生常遇到三种主要错误类型:
Syntax errors: Mistakes in the rules of the pseudocode or flowchart, such as missing an ENDIF or using an undefined symbol. These make the algorithm impossible to follow correctly.
语法错误:伪代码或流程图的规则错误,例如缺少ENDIF或使用了未定义的符号。这些错误会导致算法无法被正确执行。
Logical errors: The algorithm runs but produces the wrong output. An example is calculating the average of five numbers but dividing by 4 instead of 5.
逻辑错误:算法能够运行,但产生错误输出。例如,计算五个数的平均值却错误地除以4而非5。
Runtime errors: These occur when the algorithm is asked to perform an impossible operation, such as dividing by zero or reading a value that doesn’t exist. Good algorithms anticipate such issues with conditional checks.
运行时错误:当算法被要求执行不可能的操作时发生,例如除以零或读取不存在的值。好的算法会通过条件检查来预见这类问题。
9. Amending Algorithms – An Example | 修正算法示例
Being able to identify and fix flawed algorithms is a core skill. Consider the following algorithm meant to calculate the average of ten mass readings stored in an array mass[1..10]:
能够识别并修正有缺陷的算法是一项核心技能。考虑以下旨在计算存储在数组mass[1..10]中十个质量读数平均值的算法:
sum ← 0
FOR i ← 1 TO 10 DO
sum ← sum + mass[i]
average ← sum / 10
ENDFOR
OUTPUT average
What is the problem? The average calculation and the OUTPUT instruction are placed inside the loop. This means the average is computed and displayed ten times, and only the last iteration actually uses the correct total. The amendment is to move those lines outside the loop:
问题出在哪里?平均值的计算和OUTPUT指令被放在了循环内部。这意味着平均值会被计算并显示十次,而实际上只有最后一次迭代使用了正确的总和。修正方法是将这些行移至循环外部:
sum ← 0
FOR i ← 1 TO 10 DO
sum ← sum + mass[i]
ENDFOR
average ← sum / 10
OUTPUT average
Now the total is summed first, and the average is calculated only once. This simple amendment highlights why step-by-step logical analysis is essential.
现在先完成总和的累加,然后只计算一次平均值。这个简单的修正说明了为何逐步逻辑分析至关重要。
10. Testing and Debugging Scientifically | 科学地测试与调试
To trust an algorithm in a scientific investigation, you must test it thoroughly. Use a trace table to record the values of variables at each step for a small data set. This helps verify that the algorithm behaves as intended and uncovers hidden logic errors.
要在科学探究中信任一个算法,必须对其进行彻底测试。使用跟踪表记录一个小型数据集中每一步的变量值。这有助于验证算法是否按预期运行,并揭示隐藏的逻辑错误。
Test cases should include normal data (e.g., typical mass readings), boundary data (e.g., the minimum possible value of zero), and erroneous data (e.g., a negative mass). An algorithm that handles all these cases is robust and suitable for publication or sharing.
测试用例应包括正常数据(如典型的物质读数)、边界数据(如最小可能值零)和错误数据(如负值质量)。一个能处理所有情况的算法才是健壮的,适合发表或分享。
11. Applying Algorithms to Real Science Investigations | 将算法应用于真实的科学探究
Let’s construct a full algorithm for a classic IGCSE experiment: determining the concentration of an acid using titration. The steps must control variables and include checks for concordancy.
让我们为经典的IGCSE实验构建一个完整算法:利用滴定法测定酸的浓度。步骤必须控制变量,并包含对结果一致性的检查。
INPUT titre1, titre2
IF |titre1 − titre2| ≤ 0.2 THEN
meanTitre ← (titre1 + titre2) / 2
ELSE
INPUT titre3
IF |titre1 − titre3| ≤ 0.2 AND |titre2 − titre3| ≤ 0.2 THEN
meanTitre ← (titre1 + titre2 + titre3) / 3
ELSE
OUTPUT “Results are not concordant. Repeat all titrations.”
STOP
ENDIF
ENDIF
concentration ← (meanTitre × molarity_NaOH) / volume_acid
OUTPUT concentration
This algorithm shows how scientific decision rules can be embedded to ensure data quality. Any amendment (e.g., changing the concordancy tolerance to 0.1 cm³) must be documented and retested.
这个算法展示了如何嵌入科学决策规则以确保数据质量。任何修改(例如,将一致性容差改为0.1 cm³)都必须记录并重新测试。
12. Summary – Mastering Algorithm Skills | 总结——掌握算法技能
Writing and amending algorithms equips you with a powerful approach to problem-solving in science. Start with a clear definition of inputs and outputs, break the problem into logical steps, and then select the appropriate structures: sequence, selection, and iteration. Always test your algorithm with varied data and be prepared to amend it when errors or inefficiencies appear. Practice with flowcharts and pseudocode will make these skills second nature, preparing you not only for examinations but for any systematic scientific enquiry.
编写和修正算法为你提供了一种解决科学问题的强大方法。首先要明确定义输入和输出,将问题分解为合乎逻辑的步骤,然后选用适当的结构:顺序、选择和迭代。始终使用多样化的数据测试你的算法,并在发现错误或低效之处时准备好进行修正。通过流程图和伪代码的练习,这些技能将成为你的第二天性,不仅为考试做好准备,也为任何系统的科学探究打下基础。
Published by TutorHao | Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导