Algorithms and Flowcharts

Foundation

An algorithm is a finite, unambiguous sequence of steps that solves a problem. A flowchart draws it with five kinds of symbols — terminal, input/output, process, decision and flow lines — while pseudocode writes it in near-natural language; the two can be converted into each other.

For:Secondary 5 Information & Communication Technology · Unit:Programming concepts · Lesson 4 of 6 in the learning path

After this lesson, you can:
  • An algorithm is a finite, definite sequence of solving steps with five properties: input, output, definiteness, finiteness and effectiveness.
  • Five flowchart symbols: rounded terminals, parallelogram input/output, rectangle process, diamond decision, arrowed flow lines.
  • Sequence, decision and loop are all built from the same symbols; decision exits must be labelled Yes and No.
文體:科技知識說明文有聲朗讀

文體小知識:科技知識說明文——講解算法的意義與特性,重點訓練流程圖符號的辨認與流程圖、偽代碼之間的互相轉換。

資訊及通訊科技Information & Communication Technology
Before this lesson you should know: Web Authoring Basics

§Text Passage

1An algorithm is a definite sequence of steps for solving a particular problem: executing each step in order turns known inputs into the required output. A recipe or an assembly manual is an everyday algorithm. In computing, the algorithm precedes the program: think through the steps first, then express them in any programming language — never the other way round.

2A rigorous algorithm has five properties: zero or more inputs; at least one output; every step unambiguous, never vague; a finite number of steps, so it terminates after finitely many executions; and every step effective, actually practicable. Missing any one disqualifies it as a proper algorithm.

3A flowchart is the graphical representation of an algorithm using agreed symbols: a rounded box marks the start and end terminals; a parallelogram marks input and output; a rectangle marks processing such as assignment and calculation; a diamond marks a decision choosing paths by whether a condition holds; and arrowed flow lines connect the symbols to show the order of execution.

4Drawing an algorithm as a flowchart has two benefits: the logic becomes visible at a glance, with decisions and loops easy to locate and check; and communication becomes easy, since a flowchart is readable even to people who know no programming language — hence its use in design documents and examinations.

5Pseudocode is the textual representation of an algorithm: steps are written in near-natural language with simple keywords such as INPUT, OUTPUT, IF and WHILE, ignoring the strict syntax of any programming language. One algorithm can be drawn as a flowchart or written as pseudocode; the two structures correspond one to one, and converting between them is a frequent examination task.

1Summary 重點摘要

An algorithm is a finite, unambiguous sequence of steps that solves a problem. A flowchart draws it with five kinds of symbols — terminal, input/output, process, decision and flow lines — while pseudocode writes it in near-natural language; the two can be converted into each other.

2Vocabulary cards Vocabulary

a
algorithm算法

A finite, unambiguous sequence of steps that solves a problem, turning inputs into outputs when executed in order.

f
flowchart流程圖

A diagram of an algorithm using agreed symbols: terminal, input/output, process, decision and flow lines.

p
pseudocode偽代碼

A textual way of writing algorithm steps in near-natural keywords, free of any specific programming language syntax.

t
terminal symbol起端/終端符號

The rounded-box symbol in a flowchart marking where the algorithm starts and ends.

i
input/output symbol輸入/輸出符號

The parallelogram symbol showing data entering the algorithm or results being output to the user.

p
process symbol處理符號

The rectangle symbol representing processing steps such as assignment and calculation.

d
decision symbol判斷符號

The diamond symbol selecting a follow-up path by whether a condition holds, with exits usually labelled Yes and No.

f
flow line流程線

Arrowed lines connecting flowchart symbols and showing the order and direction of execution.

v
variable變量

A named location holding data whose value may change during execution, such as a running total or counter.

c
counter計數器

A variable tallying how many times something has happened, commonly controlling loop counts, e.g. how many marks entered.

3Step by step Steps

  1. Step 1: What an algorithm isAn algorithm is a definite sequence of steps that solves a particular problem, turning inputs into outputs when executed in order. It is not confined to computers: a recipe listing preparation and cooking steps, or an assembly manual listing installation order, is an everyday algorithm. In computing the algorithm precedes the program: think the steps through first, then write them in any programming language; languages date while the problem-solving reasoning remains universal. For example, averaging three marks — enter them, sum, divide by three, output — is a simple algorithm.
  2. Step 2: The five properties of an algorithmA rigorous algorithm shows five properties. Input: zero or more items supplied from outside. Output: at least one result, otherwise the algorithm is pointless. Definiteness: every step has a single meaning, never vague. Finiteness: the steps are finite and it must end within finite time. Effectiveness: every step is actually practicable. For example, writing 'divide the total by the class size' without stating how the size is known lacks definiteness, while 'repeat entering marks forever' violates finiteness — neither is a valid algorithm.
  3. Step 3: The five kinds of flowchart symbolsA flowchart expresses an algorithm with five agreed kinds of symbols. The rounded box is the terminal symbol marking start and end, one of each per chart. The parallelogram is the input/output symbol for data entering or results leaving. The rectangle is the process symbol carrying assignment and calculation, such as adding a mark to a total. The diamond is the decision symbol holding a condition with Yes and No exits. Flow lines with arrows connect the symbols into paths and fix the order. For example, the most common exam slips are drawing a decision as a rectangle or omitting arrowheads — match shapes to purposes carefully.
  4. Step 4: A flowchart of a sequential algorithmThe simplest algorithm is purely sequential: from the start terminal it passes through input, processing and output and ends at the terminal. To find the area of a rectangle: after the start, an input symbol reads the length and the width, a process symbol sets area to length times width, an output symbol prints the area, and flow reaches the end terminal; four symbols joined in line with no branching. For example, any task done once in fixed order — Celsius to Fahrenheit, totalling a bill — gives a straight-line chart: just arrange the symbols in input-process-output order.
  5. Step 5: Drawing a flowchart with decisionsReal problems often branch by circumstances, which is what the decision symbol is for. To judge whether a mark passes: after input, the diamond holds the condition 'mark greater than or equal to fifty'; if true the Yes exit outputs a pass, if false the No exit outputs a fail, and the two paths rejoin at the terminal. Both exits must be clearly labelled Yes and No. For example, finding the larger of two numbers is also one decision: if the first exceeds the second output the first, otherwise the second; write the condition, complete both branches, then merge — and the structure stays tidy.
  6. Step 6: Drawing loops and accumulationAlgorithms that repeat use the decision symbol to form a loop: while the condition is unmet, a flow line returns to an earlier step; once met, flow leaves the loop. To average the class marks: enter the number of students, set the total and the counter to zero; each pass enters one mark into the total and adds one to the counter; the diamond asks whether the counter is still below the number — if so, loop back to entering the next mark; if not, leave the loop, divide the total by the number, and output the average. For example, three marks of 60, 70 and 80 give a total of 210 and an average of 70; always initialise before accumulating.
  7. Step 7: Converting flowcharts to pseudocodePseudocode writes the same algorithm in words, with fixed correspondences: the input/output symbols map to INPUT and OUTPUT; the process symbol maps to assignment statements such as setting the total to zero; the diamond maps to IF or WHILE conditions; a loop opens with WHILE and its condition and closes with ENDWHILE. Convert symbol by symbol, keeping order and conditions identical. For example, the averaging chart becomes: input the number; set total and counter to zero; while the counter is below the number, input a mark, add it to the total, add one to the counter; after the loop, output the total divided by the number — a one-to-one match with the chart.

4Diagrams Diagrams

流程圖符號:四種磚頭 Flowchart symbols開始/結束處理 process判斷?輸入輸出圓角方形菱形平行四邊形例:計平均分流程開始 → 輸入三科分數 → 相加÷3(處理)→ 顯示結果 → 結束規矩:箭嘴表示行嘅次序;判斷菱形一定要有兩條出路(是/否)畫流程圖=諗清楚先寫 code——程式設計師嘅草稿
Fig. 1: Flowchart symbols

5Worked example Example

Conversion task: a flowchart shows an algorithm computing the class average with this structure — start; input the number of students N; set total T and counter C to zero; input a mark S; add S to T; add one to C; decide whether C is less than N, looping back to the input step if yes, otherwise computing A as T divided by N; output A; end. Rewrite this flowchart as pseudocode.

  1. Match symbols to keywords: input/output symbols become INPUT and OUTPUT; rectangle processes become assignments; the diamond becomes the WHILE condition.
  2. Write the opening: INPUT N first, then set T and C to zero — initialise before accumulating, or the result is meaningless.
  3. Write the loop: WHILE C is less than N, repeat three steps — INPUT S, add S to T, add one to C; when the condition fails the loop ends, and C growing each pass keeps the loop finite.
  4. Write the ending: after the loop, set A to T divided by N, OUTPUT A, and the algorithm finishes.
  5. Answer: Pseudocode: INPUT N; set T to 0; set C to 0; WHILE C < N: INPUT S; set T to T + S; set C to C + 1; ENDWHILE; set A to T / N; OUTPUT A. Key points: variables are initialised first; the loop condition matches the flowchart diamond; the counter grows every pass so the algorithm is finite; the average is computed only after leaving the loop.

6Common mistakes Mistakes

✗ Drawing a decision step as a rectangle.
✓ Decisions must use the diamond with clearly labelled Yes and No exits; rectangles only represent processing.
✗ Drawing flow lines without arrowheads.
✓ Flow lines must carry arrowheads showing the direction of execution; without them the order is unclear and the chart non-standard.
✗ Accumulating into the total without setting it to zero first.
✓ Initialise before accumulating: set the total and the counter to zero, or the starting point is undefined and the result wrong.
✗ Writing the loop condition as 'counter greater than the number' to keep entering marks.
✓ For a class average the condition is 'counter less than the number'; reversing it stops the loop from ever running.
✗ Computing and outputting the average inside the loop.
✓ Compute the average after leaving the loop with the complete total; outputting inside the loop yields several wrong intermediate values.

7Quick check Quick check

Q1In a flowchart, what does the diamond symbol represent?

Q2Which kind of symbol is used to add a mark into a running total?

Q3The total of six class marks is 402. What is the average?

Q4What does the finiteness of an algorithm mean?

Q5In pseudocode, which part of a flowchart do WHILE and ENDWHILE correspond to?

8Recap Recap

  • An algorithm is a finite, definite sequence of solving steps with five properties: input, output, definiteness, finiteness and effectiveness.
  • Five flowchart symbols: rounded terminals, parallelogram input/output, rectangle process, diamond decision, arrowed flow lines.
  • Sequence, decision and loop are all built from the same symbols; decision exits must be labelled Yes and No.
  • Initialise before accumulating: set total and counter to zero, and update the loop variable every pass.
  • Pseudocode and flowcharts map one to one: INPUT/OUTPUT to the I/O symbols, WHILE to the decision loop.

Other subjects in this grade

Same subject, other grades