Flow Charts
Flow chart
A visual representation of an algorithm, using symbols to show the sequence, decisions, and processes involved in solving a problem.
- Flow charts are a powerful tool for visualising algorithms.
- They use standardised symbols to represent different types of actions or decisions.
- By following the flow of the chart, you can trace how an algorithm works step by step.
- Think of a flow chart as a roadmap for an algorithm.
- Just as a map guides you through a journey, a flow chart guides you through the steps of a solution.
Flow charts are especially useful for tracing algorithms and ensuring correctness before implementation.
In a bank, a flow chart might represent the steps for approving a loan, including checks for credit score and income verification.
Key Symbols in Flow Charts
| Symbol | Meaning |
|---|---|
| Oval | Start/End |
| Rectangle | Process (e.g., calculations, assignments) |
| Diamond | Decision (e.g., conditionals) |
| Parallelogram | Input/Output |
| Arrow | Flow of Control |
Analysis and Tracing
Consider a flow chart that calculates the sum of the first $N$ natural numbers.
Step-by-Step Analysis:
- Initialisation: The algorithm starts by setting sum = 0 and index = 1.
- Loop Condition: The decision diamond checks if index ≤ N.
- Process Inside Loop:
- If true, the sum is updated by adding index.
- index is incremented by 1.
- Loop Continuation: The flow returns to the decision diamond to re-evaluate the condition.
- Termination: When index > n, the loop ends, and the sum is output.
Let's now assume that $N=5$:
When tracing a flow chart, use a table to track variable values at each step.
| Step | i | sum | Condition | Action |
|---|---|---|---|---|
| 1 | 1 | 0 | 1 ≤ n | sum = 0 + 1 = 1, i = 2 |
| 2 | 2 | 1 | 2 ≤ n | sum = 1 + 2 = 3, i = 3 |
| 3 | 3 | 3 | 3 ≤ n | sum = 3 + 3 = 6, i = 4 |
| 4 | 4 | 6 | 4 ≤ n | sum = 6 + 4 = 10, i = 5 |
| 5 | 5 | 10 | 5 ≤ n | sum = 10 + 5 = 15, i = 6 |
| 6 | 6 | 15 | 6 > n | output sum = 15 |
A common mistake is to increment the loop variable incorrectly, leading to infinite loops or missed iterations.
- Can you trace a flow chart to determine the final output?
- How would you modify a flow chart to handle different input values?
- What are the advantages of using flow charts over pseudocode?