This chapter explores how problems are analyzed, broken down into parts, and solved using algorithms (算法 suàn fǎ). You will learn how to use flowcharts (流程图 liú chéng tú), pseudocode (伪代码 wěi dài mǎ), and trace tables (追踪表 zhuī zōng biǎo) to design and test solutions effectively.
An algorithm (算法 suàn fǎ) is a step-by-step set of instructions to solve a problem. We use algorithms in daily life, such as following a recipe or solving a math equation. In computer science (计算机科学 jì suàn jī kē xué), algorithms are used to design programs (程序 chéngxù) that perform tasks efficiently.
The program development life cycle consists of four key stages:
Before solving a problem, it must be clearly defined (明确定义 míng què dìng yì). This includes:
Identifying inputs (输入 shū rù), processes (处理 chǔ lǐ), and outputs (输出 shū chū).
Using decomposition (分解 fēn jiě) to break complex problems into smaller manageable parts (可管理部分 kě guǎn lǐ bù fèn).
The solution is planned using:
Flowcharts (流程图 liú chéng tú) to show the sequence of steps.
Pseudocode (伪代码 wěi dài mǎ) to describe the logic.
Structure diagrams (结构图 jié gòu tú) for modular breakdown.
The solution is written in a programming language (编程语言 biān chéng yǔ yán) like Python, C++, or Java.
The program is tested to ensure it works correctly (正确 zhèng què). Errors are found using:
Syntax errors (语法错误 yǔ fǎ cuò wù) – mistakes in programming language rules.
Logic errors (逻辑错误 luó jí cuò wù) – incorrect program behavior.
Runtime errors (运行时错误 yùn xíng shí cuò wù) – errors occurring during execution.
A computer system (计算机系统 jì suàn jī xì tǒng) consists of hardware (硬件 yìng jiàn), software (软件 ruǎn jiàn), data (数据 shù jù), communications (通信 tōng xìn), and people (人 rén). Each system can be broken down (分解 fēn jiě) into sub-systems (子系统 zǐ xì tǒng), which can be further divided until each performs a single action (单一操作 dān yī cāo zuò).
Computer Systems and Sub-Systems
To understand how a computer system (计算机系统 jì suàn jī xì tǒng) works, it is often divided into sub-systems (子系统 zǐ xì tǒng) using top-down design (自顶向下设计 zì dǐng xiàng xià shè jì). This method simplifies complex systems into manageable parts (可管理部分 kě guǎn lǐ bù fèn). Each sub-system can be developed separately using:
Flowcharts (流程图 liú chéng tú) – Visual step-by-step representation.
Pseudocode (伪代码 wěi dài mǎ) – Written logic similar to programming code.
Structure diagrams (结构图 jié gòu tú) – A hierarchical breakdown of components.
Decomposing a Problem (问题分解 wèn tí fēn jiě)
Every computer system works with four main parts:
Inputs (输入 shū rù) – Data entered into the system.
Processes (处理 chǔ lǐ) – Actions performed using input data.
Outputs (输出 shū chū) – Information displayed to users.
Storage (存储 cún chú) – Data saved for future use.
💡 Example: Alarm App (闹钟应用 nào zhōng yìng yòng)
The alarm app can be decomposed (分解 fēn jiě) as follows:
Inputs (输入 shū rù) – Setting/removing alarm, pressing snooze.
Processes (处理 chǔ lǐ) – Checking time, managing snooze.
Outputs (输出 shū chū) – Sound/tune when alarm rings.
Storage (存储 cún chú) – Saving alarm time.
Methods to Design and Solve Problems
To design solutions, we use:
Structure diagrams (结构图 jié gòu tú) – Shows a breakdown of system components.
Flowcharts (流程图 liú chéng tú) – Step-by-step visual representation.
Pseudocode (伪代码 wěi dài mǎ) – Simple code-like instructions.
These methods help in systematic problem-solving (系统问题解决 xì tǒng wèn tí jiě jué) and make it easier for programmers (程序员 chéngxù yuán) to develop and test solutions efficiently.
The alarm app (闹钟应用 nào zhōng yìng yòng) can be divided into three main sub-systems (子系统 zǐ xì tǒng):
1️⃣ Setting the Alarm (设置闹钟 shè zhì nào zhōng)
🔹 Set alarm time (设定闹钟时间 shè dìng nào zhōng shí jiān)
🔹 Edit or delete alarm (编辑或删除闹钟 biān jí huò shān chú nào zhōng)
🔹 Enable or disable alarm (启用或禁用闹钟 qǐ yòng huò jìn yòng nào zhōng)
2️⃣ Checking the Alarm Time (检查闹钟时间 jiǎn chá nào zhōng shí jiān)
🔹 Continuously check the current time (持续检查当前时间 chí xù jiǎn chá dāng qián shí jiān)
🔹 Compare with alarm time (与闹钟时间比较 yǔ nào zhōng shí jiān bǐ jiào)
🔹 Activate snooze if enabled (如果启用,则激活贪睡 rú guǒ qǐ yòng, zé jī huó tān shuì)
3️⃣ Sounding the Alarm (播放闹钟 bō fàng nào zhōng)
🔹 Play alarm sound (播放闹钟声音 bō fàng nào zhōng shēng yīn)
🔹 Vibrate if enabled (如果启用,则震动 rú guǒ qǐ yòng, zé zhèn dòng)
🔹 Stop when dismissed (关闭闹钟 guān bì nào zhōng)
💡 Using a Structure Diagram (结构图 jié gòu tú)
A structure diagram (结构图 jié gòu tú) helps visualize these sub-systems (子系统 zǐ xì tǒng) and their components, making the design clearer.
A flowchart (流程图 liú chéng tú) is a diagram (图表 tú biǎo) that visually represents the steps (步骤 bù zhòu) needed to complete a task. These steps follow a specific order (顺序 shùn xù) and together form an algorithm (算法 suàn fǎ).
Flowcharts help to:
✅ Visualize (可视化 kě shì huà) how an algorithm works.
✅ Show logical flow (显示逻辑流程 xiǎn shì luó jí liú chéng) in a structured way.
✅ Make debugging easier (更容易调试 gèng róng yì tiáo shì).
The flowchart below shows how an alarm system continuously checks (持续检查 chí xù jiǎn chá) the current time until it matches the alarm time (闹钟时间 nào zhōng shí jiān).
Flowchart Steps:
1️⃣ Start (开始 kāi shǐ)
2️⃣ Get the current time (获取当前时间 huò qǔ dāng qián shí jiān)
3️⃣ Compare with alarm time (与闹钟时间比较 yǔ nào zhōng shí jiān bǐ jiào)
4️⃣ If time matches (如果匹配 rú guǒ pǐ pèi) → Sound the alarm (播放闹钟 bō fàng nào zhōng)
5️⃣ If not (否则 fǒu zé) → Wait and check again (等待并重新检查 děng dài bìng chóng xīn jiǎn chá)
6️⃣ Stop (结束 jié shù) when alarm is dismissed
This loop (循环 xún huán) repeats until the alarm rings, making sure the system operates correctly (正确运行 zhèng què yùn xíng).
🔹 Symbol: Oval (椭圆 tuǒ yuán)
🔹 Purpose: Marks the start and end of a flowchart.
✅ Example:
🟢 Start (开始 kāi shǐ) → Algorithm runs → 🔴 End (结束 jié shù)
🔹 Symbol: Rectangle (矩形 jǔ xíng)
🔹 Purpose: Represents calculations or actions.
✅ Example:
📥 Input time (输入时间 shū rù shí jiān) → 🖥 Compare with alarm time (比较闹钟时间 bǐ jiào nào zhōng shí jiān)
🔹 Symbol: Parallelogram (平行四边形 píng xíng sì biān xíng)
🔹 Purpose: Represents user input or system output.
✅ Example:
📝 Enter time (输入时间 shū rù shí jiān) → 🔊 Display alarm message (显示闹钟信息 xiǎn shì nào zhōng xìn xī)
🔹 Symbol: Diamond (菱形 líng xíng)
🔹 **Purpose: Represents yes/no (是/否 shì / fǒu) decisions in a flowchart.
✅ Example:
⏳ Is time equal to alarm time? (当前时间=闹钟时间? dāng qián shí jiān = nào zhōng shí jiān?)
✔ Yes (是 shì) → Sound alarm (播放闹钟 bō fàng nào zhōng)
❌ No (否 fǒu) → Wait and check again (等待并检查 děng dài bìng jiǎn chá)
🔹 Symbol: ➝
🔹 **Purpose: Shows the flow (流程 liú chéng) of steps in the algorithm.
✅ Example:
🔘 Start → 📥 Input → 🖥 Process → 🔀 Decision → 🔊 Output → 🔴 End
Flowcharts make algorithms easy to understand (使算法易于理解 shǐ suàn fǎ yì yú lǐ jiě).
Using these symbols (符号 fú hào) ensures clarity (清晰性 qīng xī xìng) in system design.
Pseudocode is a simple way (简单方法 jiǎn dān fāng fǎ) to describe an algorithm (算法 suàn fǎ) using English-like instructions (类英语指令 lèi yīng yǔ zhǐ lìng). It is similar to a high-level programming language (高级编程语言 gāo jí biān chéng yǔ yán) but does not follow strict syntax rules (不受严格语法规则限制 bù shòu yán gé yǔ fǎ guī zé xiàn zhì).
🔹 Why use pseudocode?
✅ Easy to read (易读 yì dú)
✅ Focuses on logic (专注逻辑 zhuān zhù luó jí)
✅ Works across different programming languages (适用于不同的编程语言 shì yòng yú bù tóng de biān chéng yǔ yán)
To ensure clarity (清晰性 qīng xī xìng), follow these rules:
📌 Keywords (关键字 guān jiàn zì) like INPUT, OUTPUT, IF, and WHILE are written in capital letters (大写字母 dà xiě zì mǔ).
📌 Variable names (变量名 biàn liàng míng) start with a capital letter (大写字母 dà xiě zì mǔ).
📌 Indentation (缩进 suō jìn) is used for loops (循环 xún huán) and conditional statements (条件语句 tiáo jiàn yǔ jù).
To assign a value (值 zhí) to a variable (变量 biàn liàng), we use ←:
📌 Variable ← Expression
🔹 Examples (示例 shì lì):
Cost ← 10 // Cost has the value 10
Price ← Cost * 2 // Price is 20
Tax ← Price * 0.12 // Tax is 2.4
SellingPrice ← Price + Tax // SellingPrice is 22.4
Gender ← "M" // Gender is "M"
Chosen ← False // Chosen is False
+ Add (加 jiā) Total ← A + B
- Subtract (减 jiǎn) Difference ← A - B
* Multiply (乘 chéng) Product ← A * B
/ Divide (除 chú) Quotient ← A / B
^ Raise to the power (指数运算 zhǐ shù yùn suàn) Square ← A ^ 2
( ) Grouping (分组 fēn zǔ) Result ← (A + B) * C
💡 Summary:
Pseudocode helps design algorithms (设计算法 shè jì suàn fǎ) before writing real code (实际代码 shí jì dài mǎ).
It is easy to read (易读 yì dú) and not language-specific (不依赖特定语言 bù yī lài tè dìng yǔ yán).
Using consistent formatting (使用一致的格式 shǐ yòng yī zhì de gé shì) makes it easier to understand.
Conditional statements allow an algorithm (算法 suàn fǎ) to make decisions (决策 jué cè) based on variable values.
Used when a condition is true (真 zhēn) or false (假 jiǎ).
The THEN path (那么 nà me) runs if the condition is true, otherwise, the ELSE path (否则 fǒu zé) runs.
The statement ends with ENDIF (结束 jié shù).
✅ Example:
IF Found THEN
OUTPUT "Your search was successful"
ELSE
OUTPUT "Your search was unsuccessful"
ENDIF
💡 If "Found" is TRUE, it prints success; otherwise, it prints unsuccessful.
Comparison Operators (比较运算符 bǐ jiào yùn suàn fú)
✅ Example using multiple conditions:
IF ((Height > 1) OR (Weight > 20)) AND (Age < 70) AND (Age > 5) THEN
OUTPUT "You can ride"
ELSE
OUTPUT "Too small, too young or too old"
ENDIF
💡 This checks height, weight, and age to decide if someone can ride.
IF ... THEN ... ELSE ... ENDIF is used for two choices (二选一 èr xuǎn yī).
CASE OF ... OTHERWISE ... ENDCASE is used for multiple choices (多个选项 duō gè xuǎn xiàng).
Boolean variables (布尔变量 bù ěr biàn liàng) store TRUE or FALSE (真或假 zhēn huò jiǎ).
Comparison operators (比较运算符 bǐ jiào yùn suàn fú) help make decisions.
Using conditional statements (条件语句 tiáo jiàn yǔ jù) makes programs smarter (更智能 gèng zhì néng)!
The CASE statement (情况语句 qíng kuàng yǔ jù) selects an action based on the value of a variable (变量的值 biàn liàng de zhí).
✅ Key Features:
The program chooses a path (选择路径 xuǎn zé lù jìng) based on the variable's value (变量值 biàn liàng zhí).
OTHERWISE (否则 fǒu zé) handles unexpected inputs.
The statement ends with ENDCASE (以 ENDCASE 结束 yǐ ENDCASE jié shù).
CASE OF Choice
1 : Answer ← Num1 + Num2 // Add (加 jiā)
2 : Answer ← Num1 - Num2 // Subtract (减 jiǎn)
3 : Answer ← Num1 * Num2 // Multiply (乘 chéng)
4 : Answer ← Num1 / Num2 // Divide (除 chú)
OTHERWISE OUTPUT "Please enter a valid choice"
ENDCASE
🔹 If Choice = 1, it adds (Num1 + Num2).
🔹 If Choice = 2, it subtracts (Num1 - Num2).
🔹 If Choice = 3, it multiplies (Num1 * Num2).
🔹 If Choice = 4, it divides (Num1 / Num2).
🔹 If none of these, it outputs: "Please enter a valid choice" (请输入有效选择 qǐng shū rù yǒu xiào xuǎn zé).
✅ Easier to read (更易读 gèng yì dú) than multiple IF ... ELSE statements.
✅ Efficient (高效 gāo xiào) when checking many possible values.
✅ Better structure (更好的结构 gèng hǎo de jié gòu) for handling multiple conditions.
Using CASE OF makes algorithms clear and organized (清晰且有条理 qīng xī qiě yǒu tiáo lǐ)!
When some actions in an algorithm need to be repeated, this is called iteration (迭代 - dié dài). Loop structures (循环结构 - xún huán jié gòu) are used to perform the iteration.
Pseudocode includes three types of loop structures:
A set number of repetitions (确定的重复 - què dìng de chóng fù)
A repetition where the number of repeats is not known, but it happens at least once (至少重复一次 - zhì shǎo chóng fù yī cì)
A repetition where the number of repeats is not known, and it might never end (可能永远不会结束 - kě néng yǒng yuǎn bù huì jié shù)
The loop structures are:
FOR ... TO ... NEXT (用于固定重复次数 - yòng yú gù dìng chóng fù cì shù)
REPEAT ... UNTIL (重复...直到 - chóng fù...zhí dào)
WHILE ... DO ... ENDWHILE (当...做...直到结束 - dāng...zuò...zhí dào jié shù)
All types of loops can perform the same task. For example, displaying ten stars (显示十颗星星 - xiǎn shì shí kē xīng xīng):
Here are examples for the three different loops in pseudocode, with explanations:
This loop is used when the number of repetitions is known ahead of time. It repeats a set number of times.
Example:
FOR i = 1 TO 10
DISPLAY "*"
NEXT
Explanation: This loop repeats 10 times (from i = 1 to i = 10). Each time it runs, it displays the word "*"
This loop is used when you don't know how many times the action will be repeated, but you are sure it will happen at least once. The loop keeps running until a certain condition is met.
Example:
REPEAT
DISPLAY "Star"
UNTIL i = 10
Explanation: This loop will keep displaying "Star" until the condition (i = 10) is met. The loop will repeat at least once, and after each repetition, the value of i will increase (not shown in the pseudocode, but assumed to be updated somewhere).
This loop is used when you don’t know how many times the action will be repeated, and it might never end. It keeps running as long as the condition is true.
Example:
WHILE i < 10 DO
DISPLAY "Star"
i = i + 1
ENDWHILE
Explanation: This loop keeps displaying "Star" as long as i is less than 10. After each iteration, i is increased by 1. If i reaches 10, the loop will stop.
All three loops are used for repetition, but they differ in how they decide when to stop the repetition. The FOR ... TO ... NEXT loop runs a fixed number of times, the REPEAT ... UNTIL loop.
The ability to repeat existing methods is very important in the design of algorithms. When an algorithm is turned into a program, the same methods may be used many times.
Here are some standard methods used in algorithms:
这五种算法是计算机科学中常用的基本方法,帮助解决常见的问题。总和 (Totalling) 算法用于累加一系列值,例如计算所有学生的总成绩。计数 (Counting) 算法则用于统计某个事件发生的次数,比如统计有多少学生通过了考试。最大值、最小值和平均值 (Maximum, Minimum, and Average) 算法帮助找到一组数据中的最大值、最小值或计算平均值,例如找出班级中成绩最高和最低的学生,或计算全班的平均成绩。线性搜索 (Linear Search) 算法用于在列表中查找某个特定值,它逐个检查列表中的元素,直到找到匹配项为止。最后,冒泡排序 (Bubble Sort) 算法通过反复比较并交换相邻元素,最终将列表排序好。这些算法在程序设计中非常有用,能帮助解决许多实际问题。
Totalling (总和 - zǒng hé) Totalling means keeping a running total that values are added to. For example, keeping track of the total marks awarded to each student in a class.
Example:
total = 0
FOR each student IN class
total = total + student_marks
NEXT
DISPLAY total
Counting (计数 - jì shù) Counting is keeping track of the number of times an action is performed. For example, counting how many students received a pass mark.
Example:
count = 0
FOR each student IN class
IF student_marks >= pass_mark THEN
count = count + 1
ENDIF
NEXT
DISPLAY count
Finding Maximum, Minimum, and Average (最大值, 最小值, 和平均值 - zuì dà zhí, zuì xiǎo zhí, hé píng jūn zhí) Finding the largest and smallest values in a list is a common task. Similarly, finding the average (mean) value is another common method.
Example (Finding Maximum):
max = student_marks[0]
FOR each mark IN student_marks
IF mark > max THEN
max = mark
ENDIF
NEXT
DISPLAY max
Linear Search (线性搜索 - xiàn xìng sōu suǒ) A linear search checks if a value is stored in a list by going through each item one by one until a match is found.
Example:
FOR each student_name IN class_list
IF student_name = "John" THEN
DISPLAY "Found"
EXIT
ENDIF
NEXT
DISPLAY "Not Found"
Bubble Sort (冒泡排序 - mào pào pái xù) A bubble sort compares each element with the next one and swaps them if they are in the wrong order. This process continues until the list is sorted.
Example:
FOR pass = 1 TO length(list)-1
FOR i = 0 TO length(list)-pass-1
IF list[i] > list[i+1] THEN
SWAP list[i] AND list[i+1]
ENDIF
NEXT
NEXT
DISPLAY list
Totalling keeps adding values together, like keeping a total score.
Counting tracks how many times an event happens, like counting how many students passed.
Finding maximum, minimum, and average helps identify the highest and lowest values, or find the average of a set.
Linear search is used to find an item by checking each element in a list, one by one.
Bubble sort sorts a list by repeatedly swapping adjacent elements that are out of order, eventually sorting the whole list.
These methods are commonly used in many algorithms, and learning them will help you write efficient programs.
To ensure that only reasonable and accurate data is accepted, computer systems need to check each item of data before accepting it. There are two similar-sounding methods used for this:
Validation (验证 - yàn zhèng): Ensures that the data is reasonable before it is entered into the system.
Verification (核实 - hé shí): Ensures that the data does not change while being entered.
Validation is when a program automatically checks that the data is reasonable before accepting it into the system. If the data is invalid, the system will show an error message explaining why the data was rejected, and the user will be asked to enter the data again.
Types of Validation Checks (验证检查的类型 - yàn zhèng jiǎn chá de lèi xíng)
Range Check (范围检查 - fàn wéi jiǎn chá): Ensures that the data falls within a specific range (for example, ages between 0 and 120).
Example:
IF age < 0 OR age > 120 THEN
DISPLAY "Invalid age, please enter a value between 0 and 120."
ENDIF
Length Check (长度检查 - cháng dù jiǎn chá): Ensures that the data is of the correct length (for example, a phone number with 10 digits).
Example:
IF length(phone_number) != 10 THEN
DISPLAY "Invalid phone number, please enter 10 digits."
ENDIF
Type Check (类型检查 - lèi xíng jiǎn chá): Ensures that the data is of the correct type (for example, checking if a number is entered instead of text).
Example:
IF NOT isNumber(age) THEN
DISPLAY "Invalid input, please enter a number."
ENDIF
Presence Check (存在检查 - cún zài jiǎn chá): Ensures that data is provided and is not empty (for example, making sure a required field like a name is filled in).
Example:
IF name IS EMPTY THEN
DISPLAY "Name is required, please enter your name."
ENDIF
Format Check (格式检查 - gé shì jiǎn chá): Ensures that the data follows a specific format (for example, checking that an email address contains an "@" symbol).
Example:
IF NOT email CONTAINS "@" THEN
DISPLAY "Invalid email format, please enter a valid email address."
ENDIF
Check Digits (校验码 - jiào yàn mǎ): Used to validate numbers by checking a special check digit, often used for credit card numbers or barcodes.
Example:
IF NOT checkDigit(card_number) THEN
DISPLAY "Invalid card number, please check the digits."
ENDIF
These validation checks help ensure that the data entered is reasonable and fits the expected rules before being accepted by the system. Verification, on the other hand, ensures that the data stays the same during the process of entering it into the system, ensuring no mistakes or changes are made.
Verification ensures that the data has been accurately copied or transferred from one source to another. For example, it checks whether the data entered into a computer matches the data from a document or whether data transferred between parts of a system has not been altered.
Verification methods include:
Double Entry (双重输入 - shuāng zhòng shū rù): This method involves entering the same data twice and comparing the two entries to check for errors.
Example:
IF input_data1 != input_data2 THEN
DISPLAY "Error: Data entries do not match."
ELSE
DISPLAY "Data is verified."
ENDIF
Screen/Visual Check (屏幕/视觉检查 - píng mù / shì jué jiǎn chá): This method involves visually checking the data on the screen to ensure it is correct.
Example:
DISPLAY "Please check the data displayed on the screen."
IF user confirms data is correct THEN
DISPLAY "Data verified."
ELSE
DISPLAY "Please re-enter the data."
ENDIF
To check whether a solution works as expected, test data needs to be applied. Testing ensures that the system functions correctly and helps identify any errors or problems. It’s common to test each part of the system (sub-system) before testing the entire system.
Test data types include:
Normal Data (正常数据 - zhèng cháng shù jù): Data that is typical and falls within the expected range.
Example:
age = 25
IF age >= 0 AND age <= 120 THEN
DISPLAY "Valid age input"
ELSE
DISPLAY "Invalid age input"
ENDIF
Abnormal Test Data (异常数据 - yì cháng shù jù): Data that is outside the expected range, often causing errors or unexpected behavior.
Example:
age = -5 // Invalid age
IF age < 0 OR age > 120 THEN
DISPLAY "Invalid age input"
ELSE
DISPLAY "Valid age input"
ENDIF
Extreme Data (极限数据 - jí xiàn shù jù): Data that is at the very limits of what is acceptable (e.g., maximum or minimum values).
Example:
age = 120 // Maximum valid age
IF age >= 0 AND age <= 120 THEN
DISPLAY "Valid age input"
ELSE
DISPLAY "Invalid age input"
ENDIF
Boundary Data (边界数据 - biān jiè shù jù): Data that falls on the boundaries of valid input, such as the first and last acceptable values.
Example:
age = 0 // Boundary value
IF age >= 0 AND age <= 120 THEN
DISPLAY "Valid age input"
ELSE
DISPLAY "Invalid age input"
ENDIF
How to Apply Test Data (如何应用测试数据 - rú hé yìng yòng cè shì shù jù)
To thoroughly test a solution, it is important to work through the algorithm multiple times using different sets of test data. For example, normal data checks if the system handles expected inputs, while abnormal data helps ensure that the system can handle errors. Extreme and boundary data help test how the system performs with edge cases.
In pseudocode or flowcharts, the system is tested step-by-step to ensure the output is correct based on the provided test data. Each type of test data helps to identify specific issues that may arise in real-world scenarios.
Trace Tables for Algorithms (追踪表): 记录变量每次变化的表格。
To understand what an algorithm does, we need a structured way to follow its steps. This is done by recording the results of each step using test data.
A trace table is a tool where we write down the value of each variable (an item that can change) every time it changes. The process of going through the algorithm step-by-step is called a dry run. (干运行): 手动逐步执行算法的过程。
Setting Up the Trace Table:
Create a column for each variable (like A, B, C which will affect the flow of the program in loops and selections).
Create a column for the output (results shown by the algorithm).
Using Test Data:
Test data (测试数据): 用于测试算法的数据。 (like 9, 7, 3, etc.) is used to perform a dry run of the algorithm.
As you run the algorithm:
Whenever a variable's value changes, write the new value in the trace table.
Whenever a value is output, record it in the output column.
A trace table can be used to record the results from each step in an algorithm; it is used to record the value of an item (variable) each time that it changes. The manual exercise of working through an algorithm step by step is called a dry run.
A trace table is set up with a column for each variable and a column for any output. For example:
Test data is then used to dry run the flowchart and record the results on the trace table. During a dry run:
» every time the value of a variable is changed, the new value is entered in that column of the trace table.
» every time a value is output, the value is shown in the output column.
Test data: 9, 7, 3, 12, 6, 4, 15, 2, 8, 5
Quiz