UNIHIVE

Study Hub UNIPORT MEG 202

MEG 202 · Basic Software Engineering

Design of algorithms: stepwise refinement and modularity

Properties of algorithms, pseudocode and flowcharts, top-down design and modules with a worked example.

Our notes

2 min read

What an algorithm is

An algorithm is a finite sequence of precise steps that solves a problem. Properties:

  1. Finiteness: it ends after a finite number of steps.
  2. Definiteness: each step is clear and unambiguous.
  3. Input: zero or more inputs.
  4. Output: at least one result.
  5. Effectiveness: each step can actually be carried out.

Three control structures

Any algorithm can be built from (the structured programming theorem):

  • Sequence: steps one after another.
  • Selection: IF ... THEN ... ELSE.
  • Iteration: loops (WHILE, REPEAT ... UNTIL, FOR).

Pseudocode and flowcharts

Pseudocode writes the algorithm in structured English. Flowcharts draw it with standard symbols: oval (start/stop), parallelogram (input/output), rectangle (process), diamond (decision), arrows (flow).

Stepwise refinement (top-down design)

Proposed by Niklaus Wirth (1971): start with a general statement of the solution and repeatedly break each step into smaller, more detailed steps until each can be written directly in code.
Example: analyse shaft test data
Level 1:
    Read the torque readings
    Compute the statistics
    Report the results
Level 2 (refining "Compute the statistics"):
    sum ← 0; max ← first reading
    FOR each reading r
        sum ← sum + r
        IF r > max THEN max ← r
    mean ← sum / n
Each refined step is now simple enough to code.

Modularity

Divide a program into modules (functions, procedures, subroutines), each doing one well-defined job, with clear inputs and outputs.
Benefits:

  • Easier to understand, write and test (each module separately).
  • Reuse: a module to convert units or compute a mean can be used in many programs.
  • Team work: different people write different modules.
  • Easier maintenance: change one module without breaking the others.

Good modules have high cohesion (do one thing) and low coupling (depend little on each other); they communicate through parameters, not shared global variables.

Example: a module in C

    double mean(double x[], int n) {
        double sum = 0.0;
        for (int i = 0; i < n; i++)
            sum = sum + x[i];
        return sum / n;
    }
Trace with x = {2.0, 4.0, 9.0}, n = 3: sum goes 2, 6, 15; returns 15/3 = 5.0 ✓.

Exams usually ask

  • State the properties of an algorithm.
  • Develop an algorithm by stepwise refinement and draw its flowchart.
  • Explain the benefits of modularity, with cohesion and coupling.

Common mistake

Writing pseudocode that is really vague English ("process the data"). Each step at the final level must be precise enough to translate directly into code.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All MEG 202 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.