MODULE 2 • LESSON 10
Everything in this module comes together here. In an exam, or in your SBA, you will be given a case: a short description of a real situation, such as a shop, a school or a utility company, and asked to write an algorithm that solves it. There is no algorithm on the page to trace this time. You must build one from the words of the case.
This lesson gives you a step-by-step method for doing that, then works through three cases from start to finish: a tiered water bill (nested selection), a class marks summary (a sentinel loop with selection inside it) and a car park fee (rounding, a formula and a cap). You will sort the facts of a case into input, process and output, and then use a design studio to adjust a car park algorithm until it passes every test.
What you will learn
- What a good solution to a case problem must contain.
- A step-by-step method: from the case to an IPO table, variables, constructs, pseudocode, flowchart, testing and C.
- How to pick out inputs, processing, outputs and the information you do not need.
- How to decide which constructs a case needs: sequence, selection, bounded or unbounded loops.
- How to develop, test and code complete algorithms for three exam-style cases.
- How to check your answer against the marks an examiner looks for.
Part 1: What a good solution looks like
When you develop an algorithm from a case, the result must be a general solution: it must work for any valid data, not just the example values in the case. It must also have the properties you met in Lesson 04:
- clearly defined, unambiguous steps: each step has exactly one meaning (write “IF units <= 10”, not “if the usage is small”);
- a finite number of steps: every loop has a way to stop;
- a clear flow of control from one step to the next: the reader can follow exactly which step comes next, including where each IF and loop ends (ENDIF, ENDWHILE, ENDFOR).
This is also why programmers develop the logic before they write code (Lesson 01): a correct algorithm can be checked on paper in minutes, and then coded in any language; a program written without one is usually slower to get right.
Part 2: The method, step by step
| Step | What you do | What you produce |
| 1. Read the case twice | Underline the data that comes in, the results that must come out, and every rule (rates, limits, conditions). | A marked-up case |
| 2. IPO table | Sort what you underlined into Input, Process and Output. Cross out information the algorithm never uses. | An IPO table |
| 3. Variables | Give each value a meaningful name and a data type. Include counters and totals you will need. | A variable list |
| 4. Constructs | Is there a decision? (selection) Is anything repeated? (loop) Is the number of repetitions known in advance? (bounded or unbounded) | A plan of the structure |
| 5. Pseudocode | Write the main steps first (top-down), then refine each one. Initialise before loops; indent the bodies. | Pseudocode |
| 6. Flowchart (if asked) | Convert the pseudocode symbol by symbol. | A flowchart |
| 7. Test | Write a short test plan with normal, boundary and special cases, and dry run each test (Lesson 09). | Test plan and trace table |
| 8. Code (for programming tasks) | Translate into C, compile and run the same tests. | A working program |
Step 2 is where many students lose marks: they miss an input, or they treat background details as data. Practise it in Try it 1.
Try it 1Sort the case: input, process or output?
Read the case, then sort each phrase. “Not needed” is for information that does not change what the algorithm does.
Part 3: Case 1: a tiered water bill
The case. A water company bills each household on the cubic metres (units) of water it used in a month. The first 10 units cost $4.00 each. Each unit above 10, up to 30, costs $6.50. Each unit above 30 costs $9.00. A service charge of $25.00 is added to every bill. Develop an algorithm that reads a household’s account number and the units used, and outputs the account number with the total bill.
Steps 2 to 4: IPO, variables and constructs
| Input | Process | Output |
| acct (account number), units | Price the units in three bands; add the $25 service charge | acct, bill |
Variables: acct (integer), units (integer), bill (real). Constructs: sequence and nested selection to pick the band. No loop is needed for one household.
The hardest part is the arithmetic for the higher bands. Work it out with numbers first. The first 10 units always cost 10 × $4 = $40. For 25 units, the units above 10 are 25 − 10 = 15, so the bill is 40 + 15 × 6.50 = $137.50 before the service charge. For more than 30 units, the first 30 cost 40 + 20 × 6.50 = $170, and the rest cost $9 each.
Step 5: pseudocode
INPUT acct, units IF units <= 10 THEN bill ← units * 4.00 ELSE IF units <= 30 THEN bill ← 40.00 + (units - 10) * 6.50 ELSE bill ← 170.00 + (units - 30) * 9.00 ENDIF bill ← bill + 25.00 OUTPUT acct, bill
The order of the tests matters (Lesson 07): by the time the ELSE IF is reached, we already know units is more than 10, so units <= 30 is enough to describe the middle band.
Step 6: flowchart
Step 7: test plan
| Test | units | Expected bill | Why this test |
| 1 | 8 | 8 × 4 + 25 = $57.00 | Normal, lowest band |
| 2 | 10 | 40 + 25 = $65.00 | Boundary: top of band 1 |
| 3 | 11 | 40 + 6.50 + 25 = $71.50 | Boundary: start of band 2 |
| 4 | 25 | 40 + 97.50 + 25 = $162.50 | Normal, middle band |
| 5 | 30 | 40 + 130 + 25 = $195.00 | Boundary: top of band 2 |
| 6 | 40 | 170 + 90 + 25 = $285.00 | Normal, top band |
| 7 | 0 | $25.00 | Special case: no water used |
Dry running test 5: 30 is not ≤ 10, but it is ≤ 30, so bill = 40 + 20 × 6.50 = 170, plus 25 = 195. Pass. Notice that tests 2, 3 and 5 sit on the band edges, where a wrong < or <= would show.
Step 8: C
#include <stdio.h>
int main(void)
{
int acct, units;
float bill;
printf("Account number: ");
scanf("%d", &acct);
printf("Units used: ");
scanf("%d", &units);
if (units <= 10)
bill = units * 4.00;
else if (units <= 30)
bill = 40.00 + (units - 10) * 6.50;
else
bill = 170.00 + (units - 30) * 9.00;
bill = bill + 25.00;
printf("\nAccount %d: $%.2f\n",
acct, bill);
return 0;
}
Sample input: 1042 25
Output:
Account number: Units used: Account 1042: $162.50
Extending the case. If the case said "for every household, until an account number of 0 is entered, then output the number of bills and the total amount billed", you would wrap the same steps in a sentinel-controlled WHILE loop (Lesson 08), with a counter and an accumulator initialised before it. That is the pattern in Case 2.
Part 4: Case 2: a class marks summary
The case. A teacher enters the exam mark of each student in her class, entering -1 when she has finished. Marks must be from 0 to 100; any other value is rejected with a message and not used. When all marks are entered, the program outputs the number of students, the average mark, the highest mark and how many students scored 50 or more.
| Input | Process | Output |
| mark (repeatedly, ending with -1) | Validate each mark; count; total; track highest; count passes; calculate average | count, average, highest, passes (or a message if no marks) |
Constructs: the number of students is not known, and the data ends with a sentinel, so the outer structure is an unbounded, sentinel-controlled WHILE loop. Inside it there is selection for validation, for the highest mark and for passes. Variables: mark, count, total, highest, passes (integers) and avg (real).
count ← 0
total ← 0
highest ← 0
passes ← 0
INPUT mark
WHILE mark <> -1 DO
IF mark < 0 OR mark > 100 THEN
OUTPUT "Invalid mark"
ELSE
count ← count + 1
total ← total + mark
IF mark > highest THEN
highest ← mark
ENDIF
IF mark >= 50 THEN
passes ← passes + 1
ENDIF
ENDIF
INPUT mark
ENDWHILE
IF count = 0 THEN
OUTPUT "No marks entered"
ELSE
avg ← total / count
OUTPUT count, avg, highest, passes
ENDIF
Three details earn marks here. All four counters and totals are set before the loop. The next mark is read at the end of the loop body, so -1 is never processed. And the average is calculated only after the loop, and only if count is not 0. Starting highest at 0 is safe because no valid mark is below 0.
Test it. With the inputs 64, 38, 105, 71, 50, -1: 105 is rejected; count = 4; total = 223; average = 55.75; highest = 71; passes = 3 (64, 71 and 50, since 50 counts as "50 or more"). A second test with just -1 should output "No marks entered".
#include <stdio.h>
int main(void)
{
int mark, count = 0, total = 0;
int high = 0, passes = 0;
printf("Type -1 to stop.\n");
printf("Mark: ");
scanf("%d", &mark);
while (mark != -1) {
if (mark < 0 || mark > 100) {
printf("Invalid mark\n");
} else {
count = count + 1;
total = total + mark;
if (mark > high)
high = mark;
if (mark >= 50)
passes = passes + 1;
}
printf("Mark: ");
scanf("%d", &mark);
}
printf("\n");
if (count == 0) {
printf("No marks entered\n");
} else {
printf("Students: %d\n", count);
printf("Average: %.1f\n",
(float) total / count);
printf("Highest: %d\n", high);
printf("Passes: %d\n", passes);
}
return 0;
}
Sample input: 64 38 105 71 50 -1
Output:
Type -1 to stop. Mark: Mark: Mark: Invalid mark Mark: Mark: Mark: Students: 4 Average: 55.8 Highest: 71 Passes: 3
If the case also asked you to output the names of the students above the average, you would need to store every mark first, because the average is not known until the end. That calls for arrays (or parallel arrays for names and marks), which you will use in Module 3.
Part 5: Case 3: a car park fee
The case. A car park charges $10 for the first hour or part of an hour. Each additional hour, or part of an hour, costs $6. No car pays more than $40 in a day. Develop an algorithm that reads the number of minutes a car was parked and outputs the fee.
The rules hide three decisions you must get right: how to turn minutes into whole hours, rounding up; how to charge the hours after the first; and how to apply the daily maximum. In integer arithmetic, a neat way to round minutes up to hours is (minutes + 59) DIV 60: 60 minutes gives 1, but 61 gives 2.
Try it 2Design studio: the car park fee
Choose an option for each design decision. The algorithm and the test plan update straight away. Your goal: make all seven tests pass, then explain why the other options fail.
1. Turning minutes into hours
2. Fee for more than one hour
3. Daily maximum
Only one combination passes every test: round up, charge (hours − 1) extra hours, and cap at $40. Each wrong choice is caught by a specific test: 61 minutes catches the rounding, 150 minutes catches the extra-hours formula, and 600 minutes catches the missing cap. This is why a test plan is part of developing an algorithm, not something added afterwards.
#include <stdio.h>
int main(void)
{
int minutes, hours, fee;
printf("Minutes parked: ");
scanf("%d", &minutes);
hours = (minutes + 59) / 60;
if (hours <= 1)
fee = 10;
else
fee = 10 + (hours - 1) * 6;
if (fee > 40)
fee = 40;
printf("\nFee: $%d\n", fee);
return 0;
}
Sample input: 150
Output:
Minutes parked: Fee: $22
In C, dividing one int by another throws away the fraction, exactly like DIV, so (minutes + 59) / 60 rounds up.
Part 6: Exam checklist
Before you hand in an algorithm developed from a case, check it against what examiners usually award marks for:
- Every input in the case is read, and nothing extra.
- Initialisation: counters and totals set before any loop.
- The correct loop: FOR when the count is known, WHILE with a sentinel when it is not; the loop can always end.
- The correct conditions, including the boundaries ("or more" is ≥).
- Correct calculations, done in the right place (averages after the loop).
- Every required output, with the right values, in the right place (inside or after the loop).
- Clear structure: indentation and matching ENDIF, ENDWHILE and ENDFOR.
If the question asks for a flowchart, use the correct symbols, label every decision exit Yes or No, and make sure every path leads to Stop.
Try it 3Case practice
Pick a mode. "Which construct?" gives a one-line case: choose the construct at the heart of its solution. "Car park fee" asks you to apply the Case 3 rules.
Summary
| Idea | Remember |
| General solution | Works for all valid data; clear, unambiguous, finite steps; clear flow of control. |
| Method | Read, IPO, variables, constructs, pseudocode, flowchart, test, code. |
| IPO | Inputs come in, processes change or decide, outputs go out; cross out what is not needed. |
| Constructs | A decision means selection; repetition with a known count means FOR; an unknown count or a sentinel means WHILE. |
| Testing | Use boundaries and special cases; each one catches a different mistake. |
Review questions
1. State THREE properties that an algorithm developed from a case should have.
Show answer
Any three: it is a general solution (works for all valid data); its steps are clearly defined and unambiguous; it has a finite number of steps; it has a clear flow of control from one step to the next; it produces the required output.
2. A case says: "A shop records the value of each sale during the day; the cashier enters 0 at closing time. Output the number of sales and the total." Identify the input, process and output, and the type of loop needed.
Show answer
Input: the value of each sale (ending with 0). Process: count the sales and add up their values. Output: number of sales and total. Loop: unbounded, sentinel-controlled (WHILE value <> 0), because the number of sales is not known in advance.
3. Using the water tariff in Case 1, calculate the bill for 31 units.
Show answer
40 + 20 × 6.50 = 170 for the first 30 units, plus 1 × 9 = 9, total 179, plus 25 = $204.00.
4. Write pseudocode that reads the ages of the 25 passengers on a bus and outputs how many are under 12.
Show answer
young ← 0
FOR i ← 1 TO 25 DO
INPUT age
IF age < 12 THEN young ← young + 1 ENDIF
ENDFOR
OUTPUT young
5. In Case 2, explain why the average is calculated after the loop and not inside it.
Show answer
The average needs the final total and the final count, which are only known when all the marks have been entered. Calculating it inside the loop wastes work and, if output there, gives wrong partial averages. After the loop it is calculated once, and can be protected from a count of 0.
6. In Case 3, what fee should be charged for 121 minutes? Show your working.
Show answer
121 minutes rounds up to 3 hours. Fee = 10 + (3 − 1) × 6 = $22. That is under $40, so no cap applies.
7. Give TWO tests that should be in the test plan for Case 3, and explain what each one checks.
Show answer
For example: 60 minutes (expected $10) checks that exactly one hour is not rounded up to two; 600 minutes (expected $40) checks the daily maximum. Others: 61 minutes ($16) checks that part of an hour counts as a full hour.
8. Develop an algorithm: input the price of an item and a customer type ("M" for member, "N" for non-member). Members get 10% off. Any bill over $1 000 after the member discount gets a further $50 off. Output the final price.
Show answer
INPUT price, type
IF type = "M" THEN price ← price * 0.9 ENDIF
IF price > 1000 THEN price ← price - 50 ENDIF
OUTPUT price
Homework
- Taxi fares. A taxi charges $15 for the first 2 km and $4.50 for each further km or part of a km. Fares for trips at night (after 22:00) are increased by 20%. Develop the algorithm fully: IPO table, variables, pseudocode, flowchart, a test plan with at least 6 tests, and a C program.
- Extend Case 1 so that it processes many households until account number 0 is entered, then outputs the number of bills, the total billed and the largest single bill.
- Extend Case 2 to also output the lowest valid mark. What value should
loweststart at, and why can’t it start at 0? - Write a test plan for the algorithm in review question 8. Include tests on both sides of the $1 000 boundary for members and non-members.
- Pick one past-paper question from the Go further box. Answer it using the method from Part 2, then compare with the worked solution.
Mastery quiz: Developing Algorithms
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: Developing Algorithms from Case Problems
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
CAPE 2012 Unit 1 Paper 02, Question 3(c) and (d). Developing an algorithm from a short case, first in pseudocode and then as a flowchart. Do the pseudocode carefully first; the flowchart is then a symbol-by-symbol translation. A worked answer is in the Go further box.
CAPE 2024 Unit 1 Paper 02, Question 3(b)(ii). A flowchart for a case with different rates. Put the rate tests in a sensible order so each band needs only one comparison, and label every decision exit.
Specimen Unit 1 Paper 02 (2021), Question 4(a). Turning an algorithm into a flowchart. Use the correct symbols (parallelogram for input and output, rectangle for processes, diamond for decisions) and show loops returning to the decision.
CAPE 2019 Unit 1 Paper 02, Question 5, Part B. Writing a complete C program for a case, including input validation with a loop. Plan it as pseudocode first, then translate line by line.
In every case question, re-read the case at the end and tick off each input, rule and output. A missing output or an unhandled rule is the most common way to drop marks.
More worked answers are on the CS Solutions page.
Previous: Module 2 • Lesson 09 – Tracing Algorithms: Trace Tables, Test Plans and Correctness. Next: Module 3 • Lesson 01 – Programming Paradigms: Imperative, Object-Oriented, Functional and Declarative.
Syllabus link: CAPE Computer Science (CXC A19/U2/21), Problem-Solving with Computers (Unit 1, Module 2). Specific Objective 9: develop algorithms from case problems (a general solution to the case, clearly defined and unambiguous steps, finite number of steps, and flow of control from one process to another). Also revisits Specific Objective 10: the need for developing the logic of a computer program.
Video search terms
Search YouTube for: "IPO chart algorithm", "pseudocode from a problem statement", "writing algorithms exam technique", "flowchart nested if example".
Try it 4Quick quiz
Tap an answer. All eight questions are self-checks on this lesson.
Updates
2026/12/01
- Published. Previous/Next links and the mastery quiz links now use the full lesson addresses; Next now goes to Module 3 Lesson 01.
- Small script tidy-up for the site (no change to how the activities work).
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 Vedesh Kungebeharry. All rights reserved.
