Decomposition means splitting a problem into smaller tasks, each simple enough to solve on its own. Do this before you write a single line of pseudocode.
This lesson opens problem solving and algorithms. The next lesson, tracing pseudocode step by step, shows how to check what your steps really do.
How do you start on a long problem?
Ask three questions in order.
- What must go in? List every piece of data the program needs.
- What must happen to it? List each calculation or decision as its own task.
- What must come out? State exactly what the user sees.
Only after these three lists exist should you think about loops or IF statements.
Worked example: a canteen bill
A school canteen wants a program. The user enters the price of three items. If the total is RM20 or more, a 10% discount is applied. The program shows the total, the discount and the amount to pay.
| Question | Answer from the problem |
|---|---|
| In | Three item prices |
| Process | Add the three prices; check whether total is at least RM20; calculate 10% discount if so; subtract it |
| Out | Total, discount, amount to pay |
The process row splits into four small tasks: add, test the condition, calculate the discount, subtract. Each one fits in one or two lines.
Does it work on real numbers?
Test the split with prices RM8.50, RM7.00 and RM6.50. The total is 8.50 + 7.00 + 6.50 = RM22.00. Since 22.00 is at least 20, the discount is 10% of 22.00 = RM2.20, and the amount to pay is 22.00 − 2.20 = RM19.80.
Every number above came from one of the small tasks. Without the condition test, a RM15.00 bill would wrongly get a discount too.
The mistake to avoid
The common mistake is to treat the whole paragraph as one block and start writing code immediately. The student then forgets a requirement, such as the condition or the last output.
| Habit | Result |
|---|---|
| Write code from the first sentence | Discount applied to every bill |
| List tasks first, then tick each against the paragraph | Every requirement covered |
A quick check is to tick off each noun and verb in the problem statement against your task list. Anything unticked is a missing task.
Check yourself
A teacher wants a program that reads the number of days a student was present and the total school days, then shows the attendance percentage. If it is below 80%, it also prints a warning. List the tasks.
Answer
In: days present and total school days. Process: divide days present by total days and multiply by 100; test whether the result is below 80. Out: the percentage, and the warning when the test is true.
For 36 days present out of 50, the percentage is 36 ÷ 50 × 100 = 72, which is below 80, so the warning prints.
What to study next
Next, put tasks in order and check them using tracing pseudocode step by step, then turn them into a diagram with designing a flowchart. Test yourself with the section practice set.
If you want a teacher to work through paragraph-style problems with you, see online one-to-one Computer Science tuition.