Slide 2
Quick Recap of Lecture 6
5 minutes · homework check
hello.cpp
- hello.ii
- (preprocessor)
- hello.o
- (compiler)
- hello.exe
- (linker)
- RAM
- (loader)
Recap
Programming Fundamentals · Lecture 7
Slide 3
Today's Plan
50 minutes · 8 parts
Recap & homework check
5 min
Learning outcomes & a big problem
3 min
How to solve a problem: steps & properties
8 min
Top-down design & stepwise refinement
12 min
Dry runs & trace tables
9 min
Class activity: design & dry-run a fee voucher
8 min
Common mistakes & recap quiz
4 min
Homework & next lecture
1 min
Programming Fundamentals · Lecture 7
Slide 4
Learning Outcomes (SLOs)
By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)
Identify the properties of a good problem-solving technique and a good algorithm
SLO 7.1 → CLO1 · C1
Break a problem into smaller parts using top-down design and stepwise refinement
SLO 7.2 → CLO1 · C3
SLO 7.3 → CLO1 · C3
Dry run an algorithm with a trace table to find mistakes before coding
CLO1 Identify the necessary properties of good problem-solving techniques.
Programming Fundamentals · Lecture 7
Slide 5
A Big Problem: Organise a Class Trip to Lahore
Too big to do in one step — so we break it into parts
Class trip to Lahore
Permission
Transport
Budget
Plan of the day
- letter to HoD,
- parents' consent
- hire coaster,
- fix time
- fare per student,
- collect money
- Badshahi Mosque,
- Minar-e-Pakistan
Lesson: a big problem becomes easy when it is broken into small problems, and each small problem is solved one by one.
Module 1 · How to Solve a Problem
Programming Fundamentals · Lecture 7
Slide 6
Four Steps of Problem Solving
Used by engineers, doctors — and programmers
Understand
Plan
Do
Check
What is given (input)? What is wanted (output)? Make an IPO chart.
Write the steps: an algorithm, as pseudocode or a flowchart.
Turn the plan into C++ code.
Test with values whose answer you know. Dry run.
Read the question twice
Top-down design
Code, compile, run
Trace table
Real-world: a doctor listens (understand), decides a treatment (plan), gives medicine (do), and checks again after a week (check).
Module 1 · How to Solve a Problem
Programming Fundamentals · Lecture 7
Slide 7
Properties of a Good Algorithm
CLO1: identify the properties of good problem-solving techniques
Module 1 · How to Solve a Problem
Programming Fundamentals · Lecture 7
Slide 8
Spot the Problem: A Bad Algorithm
Which properties are broken?
- Bad algorithm: make chai
- 1. Boil some water.
- 2. Add tea and some sugar.
- 3. Keep stirring.
- 4. Heat it in the magic kettle.
- 5. Serve.
- Good algorithm: make chai
- 1. Boil 1 cup of water.
- 2. Add 1 tsp tea and 2 tsp sugar.
- 3. Add ½ cup milk; boil for 2 minutes.
- 4. Strain into a cup.
- 5. Serve.
Module 1 · How to Solve a Problem
Programming Fundamentals · Lecture 7
Slide 9
Top-Down Design: A Structure Chart
Problem: a farmer wants to know the profit from his wheat crop
Wheat Profit Calculator
1 Input data
2 Production
3 Income
4 Cost
5 Profit
- acres, yield,
- price, cost
total maunds
- money from
- selling wheat
- total spent
- on the crop
- income − cost,
- show results
Rule: keep breaking a box until each part is a simple step you know how to code.
Module 2 · Top-Down Design
Programming Fundamentals · Lecture 7
Slide 10
Stepwise Refinement
Add detail to each step, one level at a time
- Level 1 — main steps
- 1. Input data
- 2. Find production
- 3. Find income
- 4. Find cost
- 5. Find and show profit
- Level 2 — refine step 3
- 3. Find income
- 3.1 Need total maunds (from step 2)
- 3.2 Need price per maund (input)
- 3.3 Multiply them
- Level 3 — ready to code
- totalMaunds ← acres × yield
- income ← totalMaunds × price
- totalCost ← acres × costPerAcre
- profit ← income − totalCost
Module 2 · Top-Down Design
Programming Fundamentals · Lecture 7
Slide 11
From the Plan to C++
Each refined step becomes one or two lines of code
wheat.cpp (main part)
Output
- // 1. Input
- cin >> acres >> yieldPerAcre
- >> pricePerMaund >> 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;
- 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 × 3900 = 780,000 ✓
Module 2 · Top-Down Design
Programming Fundamentals · Lecture 7
Slide 12
Why Top-Down Design Works
Benefits you will feel in every program
Easier to think
Easier to test
You solve one small part at a time.
Check each part separately.
Real-world: Eating a whole roti — bite by bite.
Real-world: Checking each room of a house, not the whole house at once.
Teamwork
Reuse
Different people can do different boxes.
A good part can be used again (functions, Lecture 23).
Real-world: Cricket team: bowlers, batsmen, keeper.
Real-world: One recipe for dough used for roti and paratha.
Module 2 · Top-Down Design
Programming Fundamentals · Lecture 7
Slide 13
Dry Run and Trace Table
Run the algorithm on paper, like the computer would
- Dry run
- Follow each step by hand with sample input.
- Like rehearsing a speech before the real event.
- Trace table
- One column for each variable (and output).
- One row for each step.
- Write the new value when a variable changes.
Module 3 · Dry Runs & Trace Tables
Programming Fundamentals · Lecture 7
Slide 14
Example: ATM Note Counter
How many 5000, 1000, 500 and 100 notes for an amount?
- Algorithm (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 all counts, amount
notes.cpp (part)
- int n5000 = amount / 5000;
- amount = amount % 5000;
- int n1000 = amount / 1000;
- amount = amount % 1000;
- // … same for 500 and 100
Output for 7850
- 5000 x 1
- 1000 x 2
- 500 x 1
- 100 x 3
- Not paid (less than 100): 50
Module 3 · Dry Runs & Trace Tables
Programming Fundamentals · Lecture 7
Slide 15
Trace Table for amount = 7850
Fill it together — one row per step
Check: 1×5000 + 2×1000 + 1×500 + 3×100 + 50 = 7850 ✓ — and it matches the real program output.
Module 3 · Dry Runs & Trace Tables
Programming Fundamentals · Lecture 7
Slide 16
A Dry Run Catches a Bug
Classic example: swapping two values
swap_bug.cpp
swap_ok.cpp
- int a = 4, b = 7;
- a = b; // step 1
- b = a; // step 2
- int temp = a; // step 1
- a = b; // step 2
- b = temp; // step 3
Output — wrong!
Output — correct
a = 7, b = 7
a = 7, b = 4
Module 3 · Dry Runs & Trace Tables
Programming Fundamentals · Lecture 7
Slide 17
CLASS ACTIVITY · 8 MIN · IN PAIRS
Design & Dry-Run a Fee Voucher
UAF fee voucher: tuition Rs. 45,000, hostel Rs. 18,000, transport Rs. 6,000. A merit scholarship gives 25% off the TUITION only. Find the total to pay.
Your tasks
Check yourself
- 1. Draw a structure chart (top box + 3–4 sub-boxes).
- 2. Refine to Level 3 pseudocode.
- 3. Make a trace table with the values above.
- 4. Which property of a good algorithm would 'give some discount' break?
- Final total should be between Rs. 55,000 and Rs. 60,000.
- Do NOT write C++ yet — plan first!
Slide 18
Common Mistakes
Habits that waste hours in the lab
✗
"Start typing code straight away."
✓ Understand → plan → code → check. Planning saves time.
✗
"My chart box says: do everything."
✓ Keep breaking until each box is a simple step.
"A dry run just reads the code."
✓ Write every variable's value after every step.
✗
"It worked for one input, so it is correct."
✓ Test several inputs, including edge cases like 0 or 99.
✗
Programming Fundamentals · Lecture 7
Slide 19
Quick Recap
Answer aloud
Q1
List four properties of a good algorithm.
Q2
What is the difference between top-down design and stepwise refinement?
Q3
What is a trace table, and which kind of error does it help find?
Q4
Trace: x = 5; y = x * 2; x = y - 3; — final x and y?
Programming Fundamentals · Lecture 7
Slide 20
Homework
Bus fare: Faisalabad → Lahore costs Rs. 1,200 per adult; children pay half. Draw a structure chart and Level 3 pseudocode for a family's total fare.
Dry run the ATM note counter for 12,350 and for 99. Show both trace tables.
Write C++ for Q1 and test it with 2 adults and 3 children.
Find two properties broken in: "Take some numbers. Keep adding them. Print the answer."
Next lecture: Conditional statements I — if and if-else (programs that make decisions)