Edexcel International Advanced Level Computer Science, point 10.1.2
Decomposition means breaking a large problem into smaller, simpler sub-problems, solving each, and combining the solutions. In Unit 2 you must be able to use comment-first coding to describe an algorithm.
Why decompose?
- A small problem is easier to understand, solve and test.
- Parts can be reused.
- Work can be shared between people.
- It leads to modular code (9.1.3).
Decomposing a problem
- State the whole problem in one sentence.
- Split it into main steps.
- Split each step until every one is small enough to write as a short subprogram.
- Decide the inputs and outputs of each part.
Example: a quiz program
Comment-first coding
Write the algorithm as comments first, in plain English, then write code under each comment. This plans the solution, keeps you organised, and leaves explanatory comments in the finished program.
Step 1: outline in comments
# 1. Ask how many items there are
# 2. Read each price, rejecting negative prices
# 3. Add up the prices
# 4. If the total is over 50 pounds, take off 10%
# 5. Display the total to 2 decimal places
Step 2: break a step down further
# 2. Read each price, rejecting negative prices
# - repeat for each of the n items
# - keep asking while the price is below 0
# - store the price in a list
Step 3: write code under each comment
# 1. Ask how many items there are
n = int(input("How many items? "))
# 2. Read each price, rejecting negative prices
prices = []
for i in range(n):
price = float(input("Price: "))
while price < 0:
price = float(input("Can't be negative. Price: "))
prices.append(price)
# 3. Add up the prices
total = sum(prices)
# 4. If the total is over 50 pounds, take off 10%
if total > 50:
total = total * 0.9
# 5. Display the total to 2 decimal places
print(f"Total: {total:.2f}")
With 3 items priced 20, -5 (rejected, then 25) and 10, the total is 55, which is over 50, so the output is Total: 49.50.
Step 4: turn parts into subprograms
def read_prices(n): ... # step 2
def apply_discount(total): ... # step 4
def show_total(total): ... # step 5
Benefits of comment-first coding
- Plan before coding: you think through the logic first.
- Spot missing steps early.
- Code and comments match, with no later "add comments" chore.
- Easier to share and review: others see the plan.
- Easier to debug: you can check each commented step.
- It matches how the algorithm may be given in an exam: in words (10.2.1).
Worked example
Task: use comment-first coding for a program that reads a sentence and counts the vowels.
# 1. Read a sentence from the user
# 2. Set the vowel count to zero
# 3. Look at each character in turn
# 4. If it is a vowel (a, e, i, o, u, any case), add one to the count
# 5. Display the count
Code under each comment:
# 1. Read a sentence from the user
sentence = input("Sentence: ")
# 2. Set the vowel count to zero
count = 0
# 3. Look at each character in turn
for ch in sentence:
# 4. If it is a vowel (a, e, i, o, u, any case), add one to the count
if ch.lower() in "aeiou":
count = count + 1
# 5. Display the count
print("Vowels:", count)
Test: "Hello World" has the vowels e, o, o, so the output is 3.
Exam tip: when asked to decompose, give the sub-problems and say what each does. For comment-first coding, show numbered comments describing the steps in plain language, with the code beneath them. Keep the comments about the purpose, not a copy of the code.
Check yourself
What is decomposition?
Breaking a problem into smaller sub-problems that are easier to solve, then combining the solutions.
What is comment-first coding?
Writing the algorithm as comments in plain English first, then writing the code under each comment.
Give two benefits of comment-first coding
Any two of: plan the logic before coding; spot missing steps early; the code is already commented; easier to review and debug.
How does decomposition lead to modular code?
Each sub-problem can be written as its own subprogram with one job, so the program is built from small modules.