UNIT 2 • MODULE 2 • LESSON 07
Five lessons of analysis and design have produced a requirements specification, DFDs, a data dictionary, a structure chart, an ERD and screen designs for the library. Now somebody has to build it. The implementation phase turns the design into working program code. The syllabus describes this phase in two words, “coding process”, but those two words hide a lot of good practice. In this lesson you will implement the Lesson 05 structure chart in C, one step at a time, exactly as a professional team would.
What you will learn
- The main activities, tools and deliverables of the implementation phase.
- How data structures in the design become C structs and arrays.
- How a structure chart becomes C function prototypes and stubs.
- Top-down implementation with stubs, and bottom-up implementation of leaf modules.
- How to complete and run the full library program.
- How to split a program into a header file and source files that match the sub-system design.
- How to spot where code has drifted from its design.
Part 1: The coding process
| Aspect | In the implementation phase |
| Main activities | Translating each module of the design into source code; unit testing each module; code review (other programmers read the code); integrating modules into a working program; keeping versions under control. |
| Tools | Editor or IDE, compiler, debugger, version control system (to keep a history of changes and let a team share code), CASE tools that generate code skeletons from designs. |
| Techniques | Coding standards (naming, layout, comments); structured programming (sequence, selection, iteration, no jumping around); top-down implementation with stubs; bottom-up implementation with test drivers; reuse of library functions. |
| Deliverables | Source code that matches the design; compiled program; internal documentation (comments, module headers); unit test results; build or installation instructions. |
Coding standards are agreed rules that every programmer on a team follows: how to name variables and functions, how to indent, what comments every function must have, and which practices to avoid (such as magic numbers or global variables shared without reason). They make the code look as if one careful person wrote it, which makes it far easier to review and maintain. You met many of these habits in Unit 1 • Module 3 • Lesson 10 – Testing, Debugging and Good Style in C and saw why they matter in Lesson 01.
Part 2: Step 1, data structures from the design
Start with the data, because every module depends on it. The ERD (Lesson 06) and data dictionary (Lesson 04) give us BOOK and LOAN. For this program we keep only the attributes the modules need, and we use day numbers (day 1, day 2) instead of full dates to keep the arithmetic simple.
#define MAXBOOKS 4
#define MAXLOANS 10
#define LOAN_DAYS 14
struct Book {
int id;
int onLoan; /* 1 = out */
};
struct Loan {
int member; /* FK */
int book; /* FK */
int dueDay;
int returnDay; /* 0 = out */
};
The DFD data stores D2 Books and D3 Loans become two arrays, books and loans, with a counter nLoans. In a full system these would be database tables or files; arrays let us focus on the structure. The constant LOAN_DAYS comes straight from the requirements specification (“books are lent for 14 days”), so if the library changes its rule there is exactly one line to edit.
Part 3: Step 2, prototypes and stubs from the structure chart
Each box on the chart becomes one C function. Each data couple going down becomes a parameter; each data couple or flag coming up becomes a return value. Reading the chart:
| Module | Couples on the chart | C prototype |
| Find book | book down; index up | int findBook(int id); |
| Find loan | book down; index up | int findLoan(int book); |
| Days late | loan, day down; days up | int daysLate(struct Loan L, int day); |
| Record loan | member, book, day down; ok flag up | int recordLoan(int member, int book, int day); |
| Record return | book, day down | int recordReturn(int book, int day); |
| Print overdue | day down | void printOverdue(int day); |
A team using top-down implementation writes main first, with stubs for the modules below it. A stub has the correct name and parameters but does no real work: it just prints that it was called and returns a safe value. This lets the team compile and run the control logic on day one.
#include <stdio.h>
/* Stubs: names and parameters from
the structure chart, no real work. */
int findBook(int id)
{
printf(" STUB findBook(%d)\n", id);
return 0;
}
int recordLoan(int member, int book,
int day)
{
printf("STUB recordLoan(%d, %d, %d)\n",
member, book, day);
findBook(book);
return 1;
}
int recordReturn(int book, int day)
{
printf("STUB recordReturn(%d, %d)\n",
book, day);
return 1;
}
void printOverdue(int day)
{
printf("STUB printOverdue(%d)\n", day);
}
int main(void)
{
recordLoan(1001, 52001, 1);
recordReturn(52001, 9);
printOverdue(20);
return 0;
}
Output:
STUB recordLoan(1001, 52001, 1) STUB findBook(52001) STUB recordReturn(52001, 9) STUB printOverdue(20)
The output proves that the calling structure matches the chart: main calls Record loan, which calls Find book, and so on. Each stub can now be replaced by a real module, one at a time, re-running the program after each replacement.
Part 4: Step 3, the leaf modules (bottom-up)
The three modules at the bottom of the chart do not call anything else, so they can be written and tested alone. Building upward from them is bottom-up implementation. Real teams often mix both directions: stubs for the structure, then real leaf modules first.
/* findBook: index of book id, or -1. */
int findBook(int id)
{
for (int i = 0; i < MAXBOOKS; i++)
if (books[i].id == id)
return i;
return -1;
}
findBook is the linear search from Unit 2 • Module 1 • Lesson 07 – Searching in C: Linear Search and Binary Search, returning -1 for “not found” exactly as its module description in Lesson 05 said. findLoan is the same idea, but it must also skip loans that have already been returned. daysLate is a one-line calculation, protected so it never returns a negative number. Each has a header comment saying what it returns.
Part 5: Step 4, the full program
Now the middle modules are written using the leaf modules, and main drives a short, fixed set of transactions so that the output can be checked. Read the program from the bottom (main) upward, comparing each function with its box on the chart.
#include <stdio.h>
#define MAXBOOKS 4
#define MAXLOANS 10
#define LOAN_DAYS 14
/* Data structures from the ERD. */
struct Book {
int id;
int onLoan; /* 1 = out, 0 = in */
};
struct Loan {
int member; /* FK to MEMBER */
int book; /* FK to BOOK */
int dueDay;
int returnDay; /* 0 = not back */
};
/* Data stores D2 Books and D3 Loans. */
struct Book books[MAXBOOKS] = {
{52001, 0}, {52003, 0},
{52007, 0}, {52010, 0}
};
struct Loan loans[MAXLOANS];
int nLoans = 0;
/* findBook: index of book id, or -1. */
int findBook(int id)
{
for (int i = 0; i < MAXBOOKS; i++)
if (books[i].id == id)
return i;
return -1;
}
/* findLoan: index of the open loan for
a book, or -1 if none. */
int findLoan(int book)
{
for (int i = 0; i < nLoans; i++)
if (loans[i].book == book
&& loans[i].returnDay == 0)
return i;
return -1;
}
/* daysLate: days past due, 0 if none. */
int daysLate(struct Loan L, int day)
{
int late = day - L.dueDay;
return late > 0 ? late : 0;
}
/* recordLoan: 1 if ok, 0 if refused. */
int recordLoan(int member, int book,
int day)
{
int b = findBook(book);
if (b == -1 || books[b].onLoan
|| nLoans == MAXLOANS)
return 0;
books[b].onLoan = 1;
loans[nLoans].member = member;
loans[nLoans].book = book;
loans[nLoans].dueDay = day + LOAN_DAYS;
loans[nLoans].returnDay = 0;
nLoans++;
return 1;
}
/* recordReturn: 1 if ok, 0 if no loan. */
int recordReturn(int book, int day)
{
int k = findLoan(book);
if (k == -1)
return 0;
loans[k].returnDay = day;
books[findBook(book)].onLoan = 0;
return 1;
}
/* printOverdue: list open late loans. */
void printOverdue(int day)
{
int count = 0;
printf("Overdue on day %d:\n", day);
for (int i = 0; i < nLoans; i++) {
struct Loan L = loans[i];
int d = daysLate(L, day);
if (L.returnDay == 0 && d > 0) {
printf(" member %d, book %d, "
"%d days\n", L.member,
L.book, d);
count++;
}
}
if (count == 0)
printf(" none\n");
}
int main(void)
{
if (recordLoan(1001, 52001, 1))
printf("Loan ok\n");
if (!recordLoan(1002, 52001, 2))
printf("Refused: already out\n");
recordLoan(1002, 52007, 3);
recordLoan(1001, 52003, 10);
recordReturn(52007, 12);
printOverdue(20);
return 0;
}
Output:
Loan ok Refused: already out Overdue on day 20: member 1001, book 52001, 5 days
Points to notice:
recordLoanreturns the ok flag from the chart. It does not print anything itself;maindecides what to say. That keeps the module cohesive (one job: record a loan) and lets other callers react differently.- Every module has a header comment, and constants replace magic numbers.
- The leaf modules are reused:
recordReturncalls bothfindLoanandfindBook. - The transactions in
mainwere chosen as tests: a normal loan, a refused loan of a book already out, a return, and an overdue check where one loan is late, one is returned and one is not yet due.
Try it 1Trace the library program
Step through main. Predict each result before you reveal it.
Output
Part 6: Step 5, files that match the sub-systems
In a real system, the Loans sub-system would live in its own source file, with a header file listing only the modules other parts of the system may call. The helper modules are marked static, which keeps them private to the file. This is information hiding, and it lowers coupling: other sub-systems cannot depend on helpers they cannot see.
#ifndef LOANS_H
#define LOANS_H
/* Public modules of the Loans
sub-system (from the chart). */
int recordLoan(int member, int book,
int day);
int recordReturn(int book, int day);
void printOverdue(int day);
#endif
loans.c is library.c without main, with #include "loans.h" added and the three helpers marked static. main.c holds only main and includes the header:
#include <stdio.h>
#include "loans.h"
int main(void)
{
if (recordLoan(1001, 52001, 1))
printf("Loan ok\n");
if (!recordLoan(1002, 52001, 2))
printf("Refused: already out\n");
recordLoan(1002, 52007, 3);
recordLoan(1001, 52003, 10);
recordReturn(52007, 12);
printOverdue(20);
return 0;
}
Compile both source files together with gcc -Wall -Wextra -std=c99 main.c loans.c -o library. The output is identical to the single-file version. This three-file layout was compiled and checked for this lesson with warnings treated as errors.
Part 7: Keeping code faithful to the design
Code that compiles and runs can still drift from its design: a different return value, a magic number where the specification set a constant, or a call to the wrong module. Drift is dangerous because other programmers rely on the design documents. When the two disagree, either the code must be fixed or the design must be formally updated. Code reviews compare each module with its design specification for exactly this reason.
Try it 2Spot the drift from the design
Each snippet compiles but breaks the design or specification. Tap the line that drifts.
Try it 3Implementation drill
"Phase" checks implementation vocabulary. "Chart to C" checks how chart symbols become code.
Common mistakes
- Coding before the design is ready, then drawing a structure chart afterwards to match the code.
- Printing inside low-level modules instead of returning a value or flag, which makes them less reusable.
- Using 0 as "not found" when 0 is a valid index.
- Leaving magic numbers that contradict the specification.
- Forgetting that a stub must still return a value of the right type, so the program compiles.
- Saying the implementation phase is "just typing code". It includes standards, unit testing, reviews and version control.
Summary
| Idea | Key point |
| Implementation | Turn the design into code; unit test; review; integrate; control versions. |
| Data first | ERD entities become structs; data stores become arrays (or files or tables). |
| Chart to code | Box becomes function; couple down becomes parameter; couple or flag up becomes return value. |
| Stubs | Top-down: write main first, stubs below; replace one at a time. |
| Files | Header for public modules; static helpers hidden; matches sub-systems. |
| Drift | Code must match the design; reviews check it. |
Review questions
1. Outline the main activities of the implementation phase.
Show answer
Translating each module of the design into code following coding standards; unit testing each module; code reviews; integrating modules; managing versions with version control.
2. Name TWO tools and TWO deliverables of the implementation phase.
Show answer
Tools: editor or IDE, compiler, debugger, version control, CASE code generators. Deliverables: source code matching the design, compiled program, internal documentation (comments), unit test results.
3. What is a stub and why is it used?
Show answer
A placeholder function with the correct name and parameters that does no real work. It lets higher-level modules be compiled and tested before lower-level modules are finished (top-down implementation).
4. Explain how a structure chart is translated into C.
Show answer
Each module becomes a function; data couples passed down become parameters; data couples and flags passed up become return values (or pointer parameters); the calling lines show which functions call which.
5. In library.c, why does recordLoan return a flag instead of printing a message?
Show answer
It keeps the module cohesive and reusable: it only records the loan, and the caller decides how to respond. It also matches the ok flag on the structure chart.
6. Why are findBook, findLoan and daysLate marked static in loans.c?
Show answer
To hide them inside the Loans sub-system so other files cannot call them. This is information hiding, which lowers coupling.
7. Give TWO examples of code drifting from its design and explain why drift matters.
Show answer
For example, returning 0 instead of -1 for "not found", or using day + 7 instead of LOAN_DAYS. Drift matters because other programmers and testers rely on the design, so mismatches cause faults and confusion.
Homework
- Add a module
countLoans(int member)that returns how many open loans a member has. Add it to the structure chart, write it in C, and call it from main. - Change recordLoan so a member may have at most 3 open loans, using a named constant. Add a test transaction to main that is refused for this reason.
- Write stubs for the sports club structure chart you drew in Lesson 05, compile them, and show the call trace.
- Split your sports club program into club.h, club.c and main.c.
Mastery quiz: The implementation phase
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: From Design to Code: The Implementation Phase in C
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.
Exam tips
The specimen papers do not have an item focused only on the implementation phase, but structured questions in this module may ask you to outline activities, tools or deliverables for any phase. For implementation, a strong answer names at least one activity (coding to standards, unit testing, review), one tool (compiler, debugger, version control) and one deliverable (source code with comments, unit test results), each with a short explanation.
In Paper 02 programming questions from Module 1, the same habits earn marks: function names that match the task, clear parameters and return values, and constants instead of unexplained numbers.
Previous: Unit 2 • Module 2 • Lesson 06 – Entity-Relationship Diagrams and User-Interface Design. Next: Unit 2 • Module 2 • Lesson 08 – Validating Software: Test Plans, Black-Box and White-Box Testing.
Syllabus link: CAPE Computer Science (CXC A19/U2/21), Software Engineering (Unit 2, Module 2). Specific Objective 6: outline the main activities, tools, techniques and deliverables of the implementation phase (the coding process).
Video search terms
Search YouTube for: "C header files and multiple source files", "top down implementation stubs", "coding standards best practices", "structure chart to code".
Try it 4Quick quiz
Tap an answer. All eight questions are self-checks on this lesson.
Updates
2026/12/01
- First version of the lesson.
© 2026 islandclass.org
