Decomposition and design tools
IGCSE Computer Science · Algorithm Design and Problem-Solving
The program development life cycle
- analysis — work out exactly what the problem is and what the program must do;
- design — plan the solution using structure diagrams, flowcharts or pseudocode;
- coding — write the program in a programming language;
- testing — run it with planned test data and correct the errors found.
The order matters, and the reason is practical: an error found in analysis costs minutes to fix; the same error found after coding may mean rewriting a large part of the program.
Decomposition
Decomposition means breaking a problem into smaller sub-problems until each is simple enough to solve. Every problem is described in terms of:
- inputs — the data that goes in;
- processes — what is done with that data;
- outputs — the results that come out.
Why it helps: each sub-problem can be written and tested separately, several people can work on different parts at once, and a part written once can be reused.
Three design tools
| structure diagram | shows the hierarchy — how the problem breaks into sub-tasks, and how those break down further |
| flowchart | shows the order of steps and the decisions taken |
| pseudocode | describes the steps in structured English, close to code but not a real language |
Flowchart symbols to know: an oval for start and stop; a parallelogram for input and output; a rectangle for a process; a diamond for a decision, with one arrow in and two out labelled yes and no.
Pseudocode is not a real language — it cannot be run. Its purpose is to let the programmer plan the logic without worrying about the exact syntax of any one language, and to communicate the design to someone who uses a different language.
Standard methods
Linear search — check each item in turn from the start until the target is found or the list ends.
It works on any list, sorted or not, but on a long list it is slow: on average it checks half the items.
Bubble sort — compare each pair of adjacent items and swap them if they are in the wrong order; repeat passes until a whole pass makes no swaps.
Note why the "no swaps" condition is the correct stopping rule: it is the only way the algorithm can know the list is fully sorted.
Totalling and counting
Two operations that appear in almost every algorithm:
- totalling — keeping a running sum: $\text{total} \leftarrow \text{total} + \text{value}$;
- counting — keeping a running count: $\text{count} \leftarrow \text{count} + 1$.
Both must be initialised to zero before the loop. Forgetting this is one of the most common errors in written algorithms.
Worked example: decomposing a problem and writing pseudocode
A program reads the marks of $30$ students, calculates the average mark, and counts how many students scored $50$ or above. (a) Decompose the problem into input, process and output. (b) Write the pseudocode. (c) State the most common error when writing an algorithm of this kind.
Solution
(a) Decomposing the problem.
Input: $30$ marks.
Process: add the marks up into a total; check each mark to see whether it is $\ge 50$ and count the students who pass; divide the total by $30$ to get the average mark.
Output: the average mark, and the number of students who pass.
(b) Pseudocode.
$\text{total} \leftarrow 0$
$\text{pass\_count} \leftarrow 0$
$\textbf{FOR } i \leftarrow 1 \textbf{ TO } 30$
$\quad \textbf{INPUT } \text{mark}$
$\quad \text{total} \leftarrow \text{total} + \text{mark}$
$\quad \textbf{IF } \text{mark} \ge 50 \textbf{ THEN}$
$\quad\quad \text{pass\_count} \leftarrow \text{pass\_count} + 1$
$\quad \textbf{ENDIF}$
$\textbf{NEXT } i$
$\text{average} \leftarrow \text{total} / 30$
$\textbf{OUTPUT } \text{average}, \text{pass\_count}$
(c) The most common error: forgetting to set variables to zero before the loop.
Both the total variable and the counter variable must be set to $0$ before the loop starts.
If forgotten: the variable will carry an undefined value — possibly garbage in memory, or a value left over from a previous run. Every accumulation afterwards adds onto a wrong base.
The dangerous thing is that the program still runs and still produces a number, but that number is wrong. An error of this kind reports nothing so it is very hard to spot.
A second common error: putting the set-to-zero statement inside the loop. Then the total is wiped back to $0$ on every iteration, so the final result equals only the last student's mark.
Worked example: bubble sort and the stopping rule
Explain how bubble sort works, and why the stopping condition must be “a pass with no swaps” rather than a fixed number of passes.
Solution
How it works.
Step 1. Start at the beginning of the list and compare two adjacent elements.
Step 2. If they are in the wrong order, swap them.
Step 3. Move on to the next pair and repeat to the end of the list. That is one pass.
Step 4. Repeat passes until one complete pass makes no swaps.
Why it is called “bubble”: after each pass, the largest element in the unsorted part drifts gradually to the end, like an air bubble rising.
Why the stopping rule must be “no swaps”.
Reason one — it is the only sign that proves the list is sorted.
If a pass goes through the whole list and makes no swaps, it means every adjacent pair is already in the right order. And if every adjacent pair is in the right order, then the whole list is in order.
This is the only way the algorithm can know it has finished — it never looks at the list as a whole.
Reason two — a fixed number of passes is either too many or too few.
A list that is almost sorted already can be finished after one pass. If we force a fixed number of passes, we waste many useless passes.
Conversely, if the number of passes is set too low, the algorithm will stop while the list is still not sorted — and it returns a wrong result without any warning.
A further point worth making. To be certain in every case, a list of $n$ elements needs at most $n-1$ passes. But using a flag that records “whether a swap occurred” is still better, because it stops as soon as it is done instead of always running the maximum number of passes.