To decompose a task, list the separate jobs it contains, give each one a single clear purpose, then put them in an order where each job has what it needs. Questions that begin “Design an algorithm that…” reward this habit before any pseudocode appears.
It is the first step of algorithm design, and the next lesson, defining inputs, outputs and constraints, makes each part precise.
How do you split a task into parts?
Read the task and underline every verb: collect, add, compare, output. Each verb usually points to a subproblem. Then ask three questions of each part.
- What does it need before it can start?
- What does it hand to the next part?
- Can I test it on its own?
If you cannot answer the third question, the part is too big or too vague.
Worked example
A teacher enters five quiz marks, each out of 20. The program must output PASS if the average mark is at least 10, otherwise FAIL.
Step 1, underline the jobs: collect five marks, add them up, find the average, compare with 10, output a word.
Step 2, name each subproblem with its input and output:
| Subproblem | Needs | Produces |
|---|---|---|
| Collect and add the marks | five marks | Total |
| Find the average | Total | Average |
| Decide pass or fail | Average | the word PASS or FAIL |
| Output | the word | text on screen |
Step 3, order them. Each row needs the one above it, so the order is fixed.
Step 4, write each part as pseudocode:
Total ← 0
FOR Count ← 1 TO 5
INPUT Mark
Total ← Total + Mark
NEXT Count
Average ← Total / 5
IF Average >= 10 THEN
OUTPUT "PASS"
ELSE
OUTPUT "FAIL"
ENDIF
Step 5, test with the marks 12, 15, 9, 18, 16. Total after each mark: 12, 27, 36, 54, 70. Average = 70 / 5 = 14. Since 14 >= 10, the output is PASS. Adding the marks again by hand gives 70, which agrees.
The mistake to watch for
A common slip is a subproblem that simply restates the task.
Mistaken plan: 1. Get the marks. 2. Work out the result. 3. Output it.
“Work out the result” hides the total, the average and the comparison, so there is nothing you can test.
The correction is to keep splitting until each line has a clear input and output, as in the table above. A second slip is putting Total ← 0 inside the loop. Then the total restarts at every mark and the final average uses only the last mark.
Check yourself
1. A library program must read a book’s days overdue and output the fine at RM0.20 per day. List the subproblems.
Show answer
Read the days overdue. Multiply by 0.20 to get the fine. Output the fine. For 15 days the fine is 15 × 0.20 = RM3.00.
2. The plan below is out of order. Put it right: output the average, add the numbers, divide the total by the count, input the numbers.
Show answer
Input the numbers, add the numbers, divide the total by the count, output the average. Each step needs the result of the one before it.
3. In the quiz example, what would the average be if Total ← 0 were inside the loop and the marks were 12, 15, 9, 18, 16?
Show answer
The total is reset at every mark, so after the last mark Total = 16. The average is 16 / 5 = 3.2, which wrongly gives FAIL.
Where this leads next
Next, learn to define inputs, outputs and constraints for each part. The pseudocode trace trainer lets you step through algorithms like this one.
If breaking a task down is the point where you stall, our teachers can work on that with you in online one-to-one Computer Science tuition, and the practice set gives you more tasks to split.