Sub topic 6
From Problem Statement to Pseudocode with Stepwise Refinement
This chapter assumes you already write pseudocode with ← for assignment, the FOR/WHILE/IF structures, 1-based arrays, and FUNCTION definitions that take parameters and return values. Here you will use those pieces to turn a vague problem statement into pseudocode you can trace.
The Idea
Stepwise refinement is a top-down way to design an algorithm. You start with a short outline in plain English. Then you replace each step with more detailed steps, and you repeat until every line maps directly to a statement or a control structure. Each pass is called a level of refinement.
Working this way keeps each decision small. You decide what the algorithm does before you decide how each part does it.
A Worked Example
Problem statement: Given n exam scores, report how many students scored above the class average.
Level 0
State the whole task in one line:
- Report how many scores are above the average.
Level 1
Split it into steps that can each be thought about on its own:
- Compute the average of the scores.
- Count the scores greater than that average.
- Output the count.
Before going further, write down your assumptions. Step 1 divides by n, so this design requires n ≥ 1. Choices like this are easy to make silently and then forget.
Level 2
Each Level 1 step is still too vague to trace, so turn each one into a function. A function per step keeps each level's interface visible: the call shows what goes in and what comes out.
FUNCTION Average(A, n)
total ← 0
FOR i ← 1 TO n DO
total ← total + A[i]
END FOR
RETURN total / n
END FUNCTION
FUNCTION CountAbove(A, n, threshold)
count ← 0
FOR i ← 1 TO n DO
IF A[i] > threshold THEN
count ← count + 1
END IF
END FOR
RETURN count
END FUNCTION
PROCEDURE ReportAboveAverage(Scores, n)
avg ← Average(Scores, n)
OUTPUT CountAbove(Scores, n, avg)
END PROCEDUREReportAboveAverage still reads like the Level 1 outline. The details sit one level down.
When to Stop Refining
Stop when every line is one of these:
- an assignment, input, or output
- a control structure whose condition and body are already precise
- a call to a function you have already refined
If you cannot trace a line by hand, for example "find the right scores", refine it again.
Common Errors
| Error | Symptom | Fix |
|---|---|---|
| Refining too early | Loop details appear before the overall steps are agreed | Finish each level before going deeper |
| Mixed levels | One step says "compute average" and the next updates `count` | Keep the steps at one level of detail |
| Hidden assumptions | Division by zero when n = 0 | Write down preconditions at Level 1 |
| Missing interfaces | A step reads a variable no earlier step produced | Pass data through parameters and return values |
Exercises
Exercise
A Level 2 refinement contains the line "IF the score is good THEN count ← count + 1". Is it fully refined? If not, what is missing?
Show answer
No. "Good" is not a precise condition, so you cannot trace it. Replace it with a comparison such as A[i] ≥ passMark, and make passMark a parameter so its value is explicit.
Exercise
Write Level 1 steps and then Level 2 pseudocode for this problem: "Given n daily temperatures T, output how many days were warmer than the day before."
Show answer
Level 1: (1) Count the days i from 2 to n where T[i] > T[i - 1]. (2) Output the count. Level 2: count ← 0; FOR i ← 2 TO n DO; IF T[i] > T[i - 1] THEN count ← count + 1 END IF; END FOR; OUTPUT count. The loop starts at 2 because day 1 has no previous day. When n ≤ 1 the loop body never runs and the output is 0.