Decomposition and design tools

IGCSE Computer Science · Algorithm Design and Problem-Solving

The program development life cycle

  1. analysis — work out exactly what the problem is and what the program must do;
  2. design — plan the solution using structure diagrams, flowcharts or pseudocode;
  3. coding — write the program in a programming language;
  4. 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:

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 diagramshows the hierarchy — how the problem breaks into sub-tasks, and how those break down further
flowchartshows the order of steps and the decisions taken
pseudocodedescribes 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:

Both must be initialised to zero before the loop. Forgetting this is one of the most common errors in written algorithms.

Four flowchart symbols — a diamond has ONE way in, TWO ways out start oval input/output bình hành process rectangle decision hình thoi A decision drawn correctly: one arrow IN diem >= 50 ? CÓ "Pass" NOT "Fail" A diamond NEVER has just one exit — it must cover BOTH possibilities.
Drawing a diamond with only one exit is a design error: a decision must cover both possibilities, otherwise the program has an undefined path.

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.

Common mistake

Forgetting to set the total and counter variables to zero before the loop. This is the most common error when writing algorithms, and it is dangerous because the program still runs and still prints a number — it is just a wrong number, with no error message at all. If you do not set them, the variable carries an undefined value and every accumulation afterwards adds onto a wrong base. An even harder-to-see variant: 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 value of the last element. The accompanying trap when drawing flowcharts: drawing a diamond with only one exit. A decision always has one arrow in and two arrows out labelled yes and no — a missing branch means the program has an undefined path.

Key takeaway

The development life cycle runs in the order analysis → design → coding → testing, and the later an error is found the more it costs. Decomposition splits a problem into input, process, output, so that each part can be tested separately and reused. A structure diagram shows the hierarchy, a flowchart shows the sequence, pseudocode describes the logic but cannot be run. Linear search works on any list but on average checks half the elements; bubble sort stops when a pass makes no swaps. Always set the total and counter variables to zero before the loop.

← Robotics and artificial intelligence · All IGCSE Computer Science lessons · Practice IGCSE Computer Science questions (free, instant scoring) · Testing: validation, verification and test data →