UNIVERSITY OF AGRICULTURE, FAISALABAD
Department of Computer Science
LECTURE 7 · STUDENT NOTES
Problem-Solving Techniques: Top-Down Design, Stepwise Refinement & Dry Runs
CS-307 Programming Fundamentals · BSCS Semester 1
Week | Slides | Linked practical | Reference ID |
3 | Lecture7_Problem_Solving_Techniques.pptx | Practical 5 | N07 |
Learning Outcomes
SLO | By the end of this lecture you can… | Builds CLO | Bloom level |
|---|---|---|---|
SLO 7.1 | Identify the properties of a good problem-solving technique and a good algorithm | CLO1: Identify the necessary properties of good problem-solving techniques. | C1 |
SLO 7.2 | Break a problem into smaller parts using top-down design and stepwise refinement | CLO1: Identify the necessary properties of good problem-solving techniques. | C3 |
SLO 7.3 | Dry run an algorithm with a trace table to find mistakes before coding | CLO1: Identify the necessary properties of good problem-solving techniques. | C3 |
How to use these notes Read these notes after the lecture and before the practical. Section numbers (for example §3.2) are used in the question bank, so you can always find where an answer comes from. Type every code example yourself — do not copy-paste. |
1. Think Before You Type
Most beginners open the IDE and start typing. Good programmers first understand the problem and plan the steps. This lecture gives you three tools for planning: the four problem-solving steps, top-down design with stepwise refinement, and dry runs with trace tables. (Slide L07-S05)
Real-world example: a class trip to Lahore The job "organise a class trip" is too big to do in one step. Break it into permission, transport, budget and plan of the day. Then break each part again (transport → hire a coaster, fix the time). Small parts are easy to do one by one. |
2. How to Solve a Problem
2.1 Four steps of problem solving
These steps come from the mathematician George Pólya's book How to Solve It (1945). (Slide L07-S06)
Step | What you do | In programming |
|---|---|---|
1 Understand | Find what is given and what is wanted | IPO chart: input, process, output |
2 Plan | Decide the steps | Algorithm as pseudocode or a flowchart; top-down design |
3 Do | Carry out the plan | Write, compile and run the C++ code |
4 Check | Make sure the answer is right | Test with known answers; dry run with a trace table |
Real-world: a doctor listens to the patient (understand), decides a treatment (plan), gives the medicine (do), and checks again after a week (check).
2.2 Properties of a good algorithm
This is exactly what CLO1 asks: identify the properties of good problem-solving techniques. (Slide L07-S07)
Property | Meaning in simple words | ATM example |
|---|---|---|
Finite | It must stop after a limited number of steps | Stops after giving cash and the card |
Definite (unambiguous) | Every step has exactly one meaning | "Enter a 4-digit PIN", not "enter something" |
Input | Takes zero or more values from outside | PIN and amount |
Output | Gives at least one result | Cash, receipt, new balance |
Effective | Every step can really be carried out | Gives only notes it has: 500, 1000, 5000 |
Correct and general | Right answer for every valid input, not just one | Works for Rs. 500 and Rs. 50,000 |
Unambiguous means it cannot be understood in two different ways.
2.3 Spotting a bad algorithm
(Slide L07-S08)
Bad step | Property broken | Better step |
|---|---|---|
Boil some water | Definite | Boil 1 cup of water |
Add tea and some sugar | Definite | Add 1 tsp tea and 2 tsp sugar |
Keep stirring | Finite | Boil for 2 minutes |
Heat it in the magic kettle | Effective | Heat it on the stove |
3. Top-Down Design and Stepwise Refinement
3.1 Top-down design
Top-down design (also called decomposition, meaning breaking into parts) starts with the whole problem at the top and breaks it into smaller sub-problems below. The drawing is called a structure chart. (Slide L07-S09)
Problem: a farmer wants to know the profit from his wheat crop.
Figure 7.1 – Structure chart for the wheat profit calculator
Keep breaking a box until each part is a simple step you know how to code. Later (Lecture 23) each box will become a function.
3.2 Stepwise refinement
Refine means "make more exact". In stepwise refinement we add detail to each step, one level at a time, until every line can be written as C++. (Slide L07-S10)
Level 1 – main steps | Level 2 – refine step 3 | Level 3 – ready to code |
|---|---|---|
1. Input data 2. Find production 3. Find income 4. Find cost 5. Find and show profit | 3. Find income 3.1 Need total maunds (from step 2) 3.2 Need price per maund (input) 3.3 Multiply them | totalMaunds ← acres × yieldPerAcre income ← totalMaunds × pricePerMaund totalCost ← acres × costPerAcre profit ← income − totalCost |
3.3 From the plan to C++
Each refined step becomes one or two lines of code. The comments 1–5 match the boxes of the structure chart. (Slide L07-S11)
wheat.cpp #include <iostream> using namespace std;
int main() { // 1. Input double acres, yieldPerAcre, pricePerMaund, costPerAcre; cout << "Area (acres): "; cin >> acres; cout << "Yield per acre (maunds): "; cin >> yieldPerAcre; cout << "Price per maund (Rs.): "; cin >> pricePerMaund; cout << "Cost per acre (Rs.): "; cin >> costPerAcre;
// 2. Production double totalMaunds = acres * yieldPerAcre; // 3. Income double income = totalMaunds * pricePerMaund; // 4. Cost double totalCost = acres * costPerAcre; // 5. Profit and output double profit = income - totalCost; cout << "Total wheat : " << totalMaunds << " maunds" << endl; cout << "Income : Rs. " << income << endl; cout << "Total cost : Rs. " << totalCost << endl; cout << "Profit : Rs. " << profit << endl; return 0; } |
Sample run Area (acres): 5 Yield per acre (maunds): 40 Price per maund (Rs.): 3900 Cost per acre (Rs.): 90000 Total wheat : 200 maunds Income : Rs. 780000 Total cost : Rs. 450000 Profit : Rs. 330000 |
Check by hand: 5 × 40 = 200 maunds; 200 × 3900 = 780,000; 5 × 90,000 = 450,000; 780,000 − 450,000 = 330,000 ✓. (The yield, price and cost are example values.)
3.4 Why top-down design works
(Slide L07-S12)
Benefit | Why | Real-world |
|---|---|---|
Easier to think | You solve one small part at a time | Eating a roti bite by bite |
Easier to test | Each part can be checked separately | Checking each room of a house |
Teamwork | Different people can do different boxes | Bowlers, batsmen and keeper in a cricket team |
Reuse | A good part can be used again (functions) | One dough recipe for roti and paratha |
4. Dry Runs and Trace Tables
4.1 What they are
A dry run means carrying out an algorithm by hand, with real values, without a computer — like rehearsing a speech before the real event. A trace table records the value of every variable after each step. Dry runs find logic errors (Lecture 6) before you type any code. (Slide L07-S13)
1. Make one column for each variable, plus an Output column.
2. Write the starting (input) values in the first row.
3. Go step by step; write a value only when it changes.
4. Compare the final output with the answer you expect.
4.2 Example: ATM note counter
How many Rs. 5000, 1000, 500 and 100 notes are needed for an amount? The algorithm takes the biggest note first and keeps the remainder, using / and % (Lecture 2). (Slides L07-S14, S15)
Pseudocode 1 INPUT amount 2 n5000 ← amount / 5000; amount ← amount % 5000 3 n1000 ← amount / 1000; amount ← amount % 1000 4 n500 ← amount / 500; amount ← amount % 500 5 n100 ← amount / 100; amount ← amount % 100 6 OUTPUT n5000, n1000, n500, n100, amount |
Step | amount | n5000 | n1000 | n500 | n100 | Output |
|---|---|---|---|---|---|---|
1 input | 7850 | |||||
2 | 2850 | 1 | ||||
3 | 850 | 2 | ||||
4 | 350 | 1 | ||||
5 | 50 | 3 | ||||
6 | 1, 2, 1, 3, 50 |
Check: 1×5000 + 2×1000 + 1×500 + 3×100 + 50 = 7850 ✓. The real program gives the same result:
notes.cpp #include <iostream> using namespace std;
int main() { int amount; cout << "Amount to withdraw (Rs.): "; cin >> amount;
int n5000 = amount / 5000; amount = amount % 5000; int n1000 = amount / 1000; amount = amount % 1000; int n500 = amount / 500; amount = amount % 500; int n100 = amount / 100; amount = amount % 100;
cout << "5000 x " << n5000 << endl; cout << "1000 x " << n1000 << endl; cout << " 500 x " << n500 << endl; cout << " 100 x " << n100 << endl; cout << "Not paid (less than 100): " << amount << endl; return 0; } |
Sample run Amount to withdraw (Rs.): 7850 5000 x 1 1000 x 2 500 x 1 100 x 3 Not paid (less than 100): 50 |
4.3 A dry run catches a bug
Swapping two values the wrong way loses the old value of a. The trace table shows it immediately. (Slide L07-S16)
swap_bug.cpp #include <iostream> using namespace std;
int main() { int a = 4, b = 7; a = b; // step 1 b = a; // step 2 cout << "a = " << a << ", b = " << b << endl; return 0; } |
Step | a | b |
|---|---|---|
start | 4 | 7 |
a = b | 7 | |
b = a | 7 |
Output — wrong a = 7, b = 7 |
Fix: keep a copy in a third variable, like using an empty glass to swap the juice in two glasses.
swap_ok.cpp #include <iostream> using namespace std;
int main() { int a = 4, b = 7; int temp = a; // step 1 a = b; // step 2 b = temp; // step 3 cout << "a = " << a << ", b = " << b << endl; return 0; } |
Step | a | b | temp |
|---|---|---|---|
start | 4 | 7 | ? |
temp = a | 4 | ||
a = b | 7 | ||
b = temp | 4 |
Output — correct a = 7, b = 4 |
5. Word Bank (Key Terms)
Word | Simple meaning |
|---|---|
Problem solving | Finding the steps that turn the given input into the wanted output |
Algorithm | A finite, clear list of steps to solve a problem |
Unambiguous / definite | Has only one meaning |
Finite | Comes to an end |
Effective | Every step can really be done |
Top-down design (decomposition) | Breaking a big problem into smaller sub-problems |
Structure chart | A diagram of a problem and its sub-problems |
Stepwise refinement | Adding detail to each step, level by level |
Dry run | Running an algorithm by hand with sample values |
Trace table | Table of variable values after each step of a dry run |
Edge case | An unusual input at the limit, e.g. 0 or 99 |
6. Self-Check Questions
1. List the four steps of problem solving and what each means in programming.
2. List and explain any five properties of a good algorithm.
3. Which properties are broken by: "Take some numbers. Keep adding them. Print the answer."?
4. What is the difference between top-down design and stepwise refinement?
5. Draw a structure chart for a bus-fare calculator (adults pay Rs. 1,200, children half).
6. Make a trace table for the ATM note counter with amount = 12,350.
7. Trace: x = 5; y = x * 2; x = y - 3; — what are the final values?
Answers are in §2.1, §2.2, §2.3, §3.1–3.2, §3.1, §4.2 and §4.1. Homework is on Slide L07-S20.