Top-Down, Bottom-Up, Stepwise Refinement and Hierarchy Charts

MODULE 2 • LESSON 02

A small problem, such as averaging three marks, fits in your head all at once. A real system, such as software to run a school tuck shop, does not. It has to record sales, keep track of stock, and produce reports, and each of those jobs hides many smaller jobs. Programmers cope with this by breaking the problem down into pieces that are small enough to understand, solve and test one at a time.

In this lesson you will learn three approaches the CAPE syllabus names — top-down design, bottom-up design and stepwise refinement — and how to show the result as a hierarchy chart. You will refine a system one level at a time in an interactive chart, watch the same system being built top-down (with stubs) and bottom-up (with drivers), and finally turn a hierarchy chart into a C program where each box becomes a function.

What you will learn

  • Why large problems are broken into smaller sub-problems (modules).
  • How top-down design works, including the use of stubs.
  • How stepwise refinement turns a broad step into detailed steps, level by level, until each step can be coded.
  • How bottom-up design works, including the use of drivers and existing components.
  • How to read and draw a hierarchy chart, and what it does and does not show.
  • How the modules on a hierarchy chart become functions in a C program.

Part 1: Breaking a problem down

Splitting a problem into parts is often called decomposition, or “divide and conquer”. Each part is a module (or sub-problem): a piece of the solution with one clear job. When every module has been solved, putting them together solves the whole problem.

Think about organising your school’s sports day. Nobody plans it as one giant task. Someone handles the track events, someone else the field events, another group the points table, and another the canteen. Each group may split its job further: the track team needs lanes marked, starters, timekeepers and a results sheet. Software is designed the same way.

Breaking a problem down has clear benefits:

  • Each part is easier to understand. You can think about “calculate the cost” without worrying about how the daily report is printed.
  • Work can be shared. Different programmers can write different modules at the same time.
  • Testing is easier. A small module can be tested on its own, so errors are found and fixed quickly.
  • Modules can be reused. A module that looks up prices can be used by both the sales and the stock parts of the system.
  • Maintenance is easier. If VAT changes, only the module that calculates VAT needs to be updated.

The three approaches below are different ways of carrying out this decomposition, and of deciding which pieces to work on first.

Part 2: Top-down design

In top-down design you start with the whole problem and break it into a few large sub-problems. Each sub-problem is then broken into smaller sub-problems, and so on, until every piece is simple enough to solve directly. You move from the general to the specific: from the top of the chart down.

Worked example. The tuck shop system, top-down:

  • Level 1 (the whole problem): Tuck shop sales system.
  • Level 2: Record a sale; Manage stock; Produce the daily report.
  • Level 3 (Record a sale only): Input the item code and quantity; Look up the price; Calculate the cost; Update the stock level.
  • Level 4 (Calculate the cost only): cost = price × quantity; if quantity is 5 or more, take 10% off.

Notice that you do not have to break every branch down to the same depth. “Calculate the cost” needed one more level; “Input the item code and quantity” was already simple enough.

Testing top-down: stubs

When a system is built top-down, the higher-level modules are written first. But the main module calls lower modules that do not exist yet, so how can it be tested? The answer is a stub: a short, temporary module that has the right name and inputs but does very little — it might just print “price lookup not written yet” and return a fixed value. Stubs let you check that the main module calls its parts in the right order with the right values. Each stub is later replaced by the real module. Part 7 shows a C program that uses stubs.

Advantages of top-down designDisadvantages
You see the whole system and its structure early.Low-level problems (for example, a tricky calculation) may be discovered late.
It is natural for planning: big picture first, detail later.Lots of stubs may be needed before anything does real work.
Modules fit together, because the structure was designed first.Similar low-level modules in different branches may be written twice if nobody notices.
Work can be divided by branch among a team.The top levels cannot be fully tested until the lower levels exist.

Part 3: Stepwise refinement

Stepwise refinement is the process of writing a solution as a few broad steps, then rewriting (refining) each step as a sequence of more detailed steps, and repeating this until every step is simple enough to be converted directly into program code. It is how top-down design is applied to an algorithm, which is why you will often see the phrase top-down stepwise refinement.

The difference in emphasis is worth remembering for exams. Top-down design describes the overall strategy of splitting a problem into sub-problems (which can be shown on a hierarchy chart). Stepwise refinement describes what happens to the steps of the algorithm: each pass adds detail to the steps produced by the pass before.

Worked example. A teacher wants a report of each student’s average and whether they passed (an average of 50 or more).

First pass (level 1):

1. Get the marks
2. Work out the average
3. Decide pass or fail
4. Print the report line

Second pass (level 2): each step is refined.

1. Get the marks
   1.1 Input the student’s name
   1.2 Input the three marks
2. Work out the average
   2.1 total ← mark1 + mark2 + mark3
   2.2 average ← total / 3
3. Decide pass or fail
   3.1 IF average ≥ 50 THEN result ← "Pass"
   3.2 ELSE result ← "Fail"
4. Print the report line
   4.1 OUTPUT name, average, result

Third pass: step 1.2 hides a question — what if a mark is outside 0 to 100? Refining it again gives:

1.2 Input the three marks
    1.2.1 FOR each of the 3 marks
    1.2.2    INPUT mark
    1.2.3    WHILE mark < 0 OR mark > 100
    1.2.4       OUTPUT "Mark must be 0 to 100"
    1.2.5       INPUT mark
    1.2.6    ENDWHILE
    1.2.7 ENDFOR

Every step now corresponds to one or two lines of code, so refinement can stop. The numbering (1, 1.2, 1.2.3) shows which step each detail came from, which makes the algorithm easy to check against the original plan.

Try it 1Refine a system, level by level

Tap a module in the hierarchy chart (or use the Refine buttons) to break it into the next level. The text below the chart is the same design written as a stepwise refinement outline: each refined step gains numbered sub-steps.

Stepwise refinement outline



Part 4: Hierarchy charts

A hierarchy chart (also called a structure chart) is a diagram that shows how a problem has been broken into modules. The whole problem is a single box at the top. Each box is joined by lines to the boxes directly below it, which are the sub-modules it is made up of (or, in a program, the modules it calls). Detail increases as you go down.

Tuck shop sales system1 Record a sale1.1 Input itemand quantity1.2 Look upprice1.3 Calculatecost1.4 Updatestock2 Manage stock2.1 Add newstock2.2 List lowstock3 Daily report3.1 Total sales3.2 Find bestseller3.3 Printreport
A three-level hierarchy chart for the tuck shop. Sub-modules are drawn under their parent to fit a phone screen; on paper they are often drawn in a row.

Rules for drawing a hierarchy chart

  • Put the name of the whole problem (or main module) in one box at the top.
  • Use rectangles for modules and plain lines (not arrows) to join a module to its sub-modules.
  • Give every box a short name that starts with a verb where possible (“Calculate cost”, “Print report”).
  • Keep each level at roughly the same amount of detail. If one box is a whole sub-system and its neighbour is a single statement, the chart is unbalanced.
  • Number the boxes (1, 1.1, 1.2 …) if you will refer to them in pseudocode.
  • Stop when every bottom box is simple enough to code as a short function.

What a hierarchy chart does not show

A hierarchy chart shows structure: what is made of what. It does not show the order of every operation, decisions (IF), loops (WHILE), or exactly what data passes between modules. Those details belong in pseudocode or a flowchart (Lesson 05). A very common exam mistake is to draw a flowchart, with diamonds and arrows, when a hierarchy chart is asked for.

Hierarchy chartFlowchart
ShowsHow the problem is divided into modulesThe flow of control through steps
ShapesRectangles onlyTerminals, parallelograms, rectangles, diamonds
ConnectionsLines (parent to child)Arrows (direction of flow)
Decisions and loopsNot shownShown with diamonds and arrows
Reading orderTop to bottom by levelFollow the arrows from Start to Stop

Part 5: Bottom-up design

In bottom-up design you start with the smallest, most detailed pieces. Each small module is designed, coded and tested on its own. Tested modules are then combined to make larger modules, which are tested in turn, until the complete system has been built. You move from the specific to the general: from the bottom of the chart up.

Bottom-up design is natural when useful building blocks already exist. C programmers use library functions such as printf and sqrt without writing them; a company may already own a well-tested module that validates dates or calculates tax. Building on top of components like these is a bottom-up way of working. It is a little like building with Lego: you start with bricks you trust, make small parts, and join the parts into something bigger.

Testing bottom-up: drivers

A low-level module, such as “Calculate cost”, cannot run on its own: something has to call it and give it values. When the higher-level modules do not exist yet, a driver is written: a small test program whose only job is to call the module with test data and display the results. Drivers are the bottom-up partner of top-down stubs.

Advantages of bottom-up designDisadvantages
Each piece is tested thoroughly before it is used, so the building blocks are reliable.The overall structure is not visible until late; parts may not fit together well.
Existing, proven modules can be reused, saving time.Some modules may be written that the final system does not need.
Tricky low-level problems are tackled early.Drivers must be written to test modules before the higher levels exist.
Good when the low-level operations are well understood.The user sees no working system until near the end.

Try it 2Build it top-down or bottom-up

The same tuck shop design is built in two different orders. Choose an approach and tap Next step. Yellow dashed boxes are stubs; purple boxes have been tested with a driver; green boxes are coded and tested as part of the system.

All steps so far

Compare the two runs. Top-down produces a skeleton of the whole system after one step (with stubs everywhere), and fills in detail. Bottom-up produces nothing the user can run until the very last step, but every brick was tested before it was used.

Part 6: Comparing the approaches

Top-downBottom-upStepwise refinement
Starts withThe whole problemThe smallest modulesA few broad algorithm steps
DirectionGeneral to specificSpecific to generalBroad steps to detailed steps
Testing aidStubsDriversDry runs of each level
Typical pictureHierarchy chartHierarchy chart built upwardsNumbered pseudocode (1, 1.1, 1.1.1)
Best whenDesigning a new system from a clear requirementReliable components already existWriting the algorithm for any module

In practice, teams mix them. A system is usually designed top-down (so the structure is right), the algorithm for each module is written by stepwise refinement, and well-understood or reused components are built bottom-up. Knowing all three lets you choose sensibly and explain your choice.

Part 7: From hierarchy chart to C functions

In C, a module becomes a function. The main module becomes main, and each box below it becomes a function that main calls. Here is a hierarchy chart for recording one tuck shop sale, followed by its stepwise refinement and the C program.

0 Record a sale (main)1.1 Input orderread_int (code)read_int(quantity)1.2 Work out costprice_ofcost_of1.3 Print receiptreceipt
Each bottom box is the name of a C function in the program below.

Pseudocode (refined to level 2)

0 Record a sale
  1.1 Input order
      INPUT code
      INPUT qty
  1.2 Work out cost
      price ← price of item code
      cost ← price × qty
      IF qty ≥ 5 THEN cost ← cost × 0.90
  1.3 Print receipt
      OUTPUT code, qty, cost

Item codes: 1 = patty ($8.00), 2 = juice ($5.00), 3 = sweet bread ($6.00). The sample order is 6 patties, so the 10% discount applies: 6 × $8.00 = $48.00, less 10%, gives $43.20.

#include <stdio.h>

/* 1.1 Input the order */
int read_int(void)
{
    int x = 0;
    scanf("%d", &x);
    return x;
}

/* 1.2a Look up the price of an item */
double price_of(int code)
{
    if (code == 1) return 8.00; /* patty */
    if (code == 2) return 5.00; /* juice */
    if (code == 3) return 6.00; /* bread */
    return 0.00;      /* unknown */
}

/* 1.2b Calculate the cost */
double cost_of(double price, int qty)
{
    double cost = price * qty;
    if (qty >= 5)
        cost = cost * 0.90;  /* 10% off */
    return cost;
}

/* 1.3 Print the receipt */
void receipt(int code, int qty, double c)
{
    printf("Item %d x %d\n", code, qty);
    printf("Cost: $%.2f\n", c);
}

/* 0 Main module: Record a sale */
int main(void)
{
    int code = read_int();
    int qty = read_int();
    double p = price_of(code);
    receipt(code, qty, cost_of(p, qty));
    return 0;
}

Sample input: 1 6

Output:

Item 1 x 6
Cost: $43.20

Each function does one job, just like each box on the chart. If the discount rule changes, only cost_of needs to change. If you are new to C functions, Your First Function: void, Declarations, and Definitions explains how a function is declared, defined and called.

The same program built top-down, with stubs

Suppose main, read_int and receipt have been written, but nobody has written the price list or the discount rule yet. Top-down, we can still compile and test main by replacing the missing functions with stubs. The stubs announce themselves and return fixed values, so we can see that main calls them in the right order and passes the right values.

#include <stdio.h>

int read_int(void)
{
    int x = 0;
    scanf("%d", &x);
    return x;
}

/* STUB: not written yet */
double price_of(int code)
{
    printf("[stub] price_of(%d)\n", code);
    return 1.00;
}

/* STUB: not written yet */
double cost_of(double price, int qty)
{
    printf("[stub] cost_of(%.2f, %d)\n",
           price, qty);
    return 0.00;
}

void receipt(int code, int qty, double c)
{
    printf("Item %d x %d\n", code, qty);
    printf("Cost: $%.2f\n", c);
}

int main(void)
{
    int code = read_int();
    int qty = read_int();
    double p = price_of(code);
    receipt(code, qty, cost_of(p, qty));
    return 0;
}

Sample input: 1 6

Output:

[stub] price_of(1)
[stub] cost_of(1.00, 6)
Item 1 x 6
Cost: $0.00

The output proves the structure works: the code was read, the price was requested for item 1, the cost was requested for 6 items, and a receipt was printed. The $0.00 is expected, because the stub does not calculate anything yet. Replacing the two stubs with the real functions gives the full program above. A bottom-up programmer would do the opposite: write cost_of first and test it with a tiny driver main that calls, say, cost_of(8.00, 6) and prints the result.

Try it 3Which approach is it?

Read the description and pick the approach, or answer the hierarchy chart question. Tap New question for another.

Summary

IdeaKey points
DecompositionBreaking a problem into modules that are easier to understand, share, test, reuse and maintain.
Top-down designStart with the whole problem and split it into smaller and smaller sub-problems. Built and tested with stubs.
Bottom-up designStart with small, detailed modules (often existing ones), test each with a driver, and combine them into larger modules.
Stepwise refinementWrite broad steps, then refine each step into more detailed steps, repeatedly, until each step can be coded.
Hierarchy chartBoxes and lines showing how the problem is divided into modules, with the whole problem at the top. Shows structure, not flow.

Review questions

1. Explain what is meant by top-down design.

Show answer

Top-down design starts with the whole problem and breaks it into a small number of sub-problems, each of which is broken down further, until every sub-problem is simple enough to solve directly.

2. Explain what is meant by bottom-up design.

Show answer

Bottom-up design starts with the smallest, most detailed modules, which are coded and tested first. Tested modules are combined into larger modules, which are tested in turn, until the complete system is built.

3. Describe stepwise refinement, using an example.

Show answer

The solution is written as a few broad steps; each step is then rewritten as more detailed steps, and this is repeated until each step can be coded directly. Example: "Work out the average" becomes "total ← m1 + m2 + m3" and "average ← total / 3".

4. Give TWO advantages of top-down design and ONE disadvantage.

Show answer

Advantages (any two): the overall structure is clear early; modules fit together because the structure is designed first; work can be split among a team by branch. Disadvantage (any one): low-level difficulties are found late; many stubs are needed; the upper levels cannot be fully tested until the lower levels exist.

5. What is the difference between a stub and a driver?

Show answer

A stub is a temporary, simplified version of a lower-level module, used so that a higher-level module can be tested before the lower module is written (top-down). A driver is a small test program that calls a lower-level module with test data, used when the higher-level module does not exist yet (bottom-up).

6. Draw a hierarchy chart, with at least two levels below the top, for a program that manages a class library: lending books, returning books and listing overdue books.

Show answer

Top: Class library system. Level 2: Lend a book; Return a book; List overdue books. Level 3 examples: Lend → Input student ID, Input book ID, Record due date. Return → Input book ID, Mark as returned, Work out any fine. List overdue → Check each loan’s due date, Print overdue loans.

7. A student draws diamonds and arrows when asked for a hierarchy chart. Explain what is wrong.

Show answer

Diamonds and arrows belong to flowcharts, which show flow of control and decisions. A hierarchy chart uses only rectangles joined by lines to show how the problem is divided into modules; it does not show decisions or loops.

8. In the tuck shop C program, which function would change if the discount became 15% for 10 or more items? Why is this an advantage of modular design?

Show answer

Only cost_of would change. Because each module has one job, a change in one rule is confined to one module, so the rest of the program does not need to be edited or re-tested as much.

Homework

  1. Using top-down design, draw a hierarchy chart (three levels) for a program that runs a school bake sale: taking orders, recording payments and printing a summary at the end of the day.
  2. Take one module from your chart and write its algorithm by stepwise refinement, showing level 1, level 2 and level 3 with numbered steps.
  3. In Try it 2, count how many stubs exist after step 1 of the top-down build, and how many drivers have been used after step 3 of the bottom-up build. Explain why.
  4. Write a driver main for cost_of that tests the quantities 1, 4, 5 and 10 with a price of $8.00 and prints each result. Which quantity is the most important test, and why?
  5. Write a short paragraph (about 100 words) recommending top-down or bottom-up design for a new bus-pass app, and justify your choice.

Mastery quiz: Top-Down, Bottom-Up, Stepwise Refinement and Hierarchy Charts

This 10-question quiz is separate from the short Quick quiz at the end of the lesson. Score 8/10 or higher to earn this lesson’s badge. Your best score is saved in this browser on this device.

Mastery quiz: Top-Down, Bottom-Up, Stepwise Refinement and Hierarchy Charts

Your course badges: loading saved progress…

View badges

    No course badges earned yet. Score 8/10 in a lesson quiz to earn its badge.

    Saved in this browser on this device. Clearing browser data may remove your badges.

    Answer all 10 questions. Earn this lesson’s badge at 8/10 or higher. Read the explanations, revisit the Try it activities and retry as often as you need. These are self-study badges, not formal grades.

    1. Top-down design starts with...
    2. Bottom-up design starts with...
    3. Stepwise refinement means...
    4. When should stepwise refinement stop?
    5. A stub is...
    6. A driver is...
    7. In a hierarchy chart, the lines between boxes show...
    8. Which of these is NOT shown on a hierarchy chart?
    9. One advantage of bottom-up design is that...
    10. In C, a module on a hierarchy chart is usually written as...

    Exam tips

    CAPE 2024 Unit 1 Paper 02, Question 4(a). Short explanations of stepwise refinement and of the bottom-up method. For stepwise refinement, mention repeated breaking down of steps until each can be coded directly. For bottom-up, mention that the smallest modules are coded and tested first, then combined into larger modules until the whole system is complete. A worked answer is linked in the Go further box.

    Hierarchy chart questions. When asked to draw one, use rectangles and lines only, put the whole problem at the top, and show at least two levels below it unless told otherwise. Do not add Start/Stop, diamonds or arrows. Name modules with verbs.

    Compare or contrast questions. If asked to distinguish top-down from bottom-up, give the starting point and the direction for each (whole problem, general to specific; smallest modules, specific to general). Mentioning stubs and drivers is a good way to show depth.

    More worked answers are on the CS Solutions page.

    Previous: Module 2 • Lesson 01 – Problems, Solutions, Algorithms and Programs. Next: Module 2 • Lesson 03 – The Stages of the Problem-Solving Process.

    Syllabus link: CAPE Computer Science (CXC A19/U2/21), Problem-Solving with Computers (Unit 1, Module 2). Specific Objective 2: apply approaches to problem solving (top-down, bottom-up and stepwise refinement; represent solutions using a hierarchy chart).

    Video search terms

    Search YouTube for: "top-down design structure chart", "stepwise refinement example", "stubs and drivers testing", "bottom-up programming approach".

    Try it 4Quick quiz

    Tap an answer. All eight questions are self-checks on this lesson.

    Updates

    2026/12/01

    - Updated the copyright line.

    - Published. Previous/Next links and the mastery quiz links now use the full lesson addresses.

    - Small script tidy-up for the site (no change to how the activities work).

    - Fixed an activity script that the site changed when the lesson was first published.

    2026/11/24

    - Linked Previous/Next and the mastery quiz URLs to draft shortlinks (?p=ID), which will redirect to the permalink after publish.

    - First version of the lesson.

    © 2026 islandclass.org

    Leave a Reply