Duration | Language | Tools | Linked Lecture |
|---|---|---|---|
50 minutes | C++ | Dev-C++ or Code::Blocks (online compiler as backup) | Lecture 13 (and 11–12) |
Mapped outcomes: CLO4 (C++ syntax and semantics) and CLO1 (problem solving) at psychomotor level P3 – Guided Response · PLO2, PLO3, PLO5
1. Lab Objectives
By the end of this 50-minute lab, you will be able to:
- Trace for loops with different steps and directions.
- Use for loops for tables, totals, counts, highest and lowest values.
- Combine a for loop with do-while validation inside it.
- Find the stray-semicolon, wrong-start-value and off-by-one bugs.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Loop variable | The counter of a for loop | The lap number on a running track |
Step | How much the counter changes each round | Climbing stairs two at a time |
Counter | Counts how many items match a rule | Counting students who passed |
Running maximum | The largest value seen so far | The highest score in a cricket series so far |
Compound profit | Profit added to the balance before the next profit | A savings account growing month by month |
3. Background
Reminder for (start; condition; update) { // body } // sum starts at 0, product starts at 1 |
4. Time Plan (50 minutes)
Part | Activity | Time |
|---|---|---|
— | Briefing | 2 min |
A | Warm-up trace | 3 min |
B | Predict the output | 4 min |
C | Bug hunt (3 bugs) | 5 min |
D | Graded tasks 1–3 | 32 min |
E | Post-lab questions & submission | 4 min |
Part A: Warm-up Trace (3 min)
lab9_warmup.cpp #include <iostream> using namespace std;
int main() { for (int i = 1; i <= 10; i += 3) { cout << i << " "; } cout << endl; return 0; } |
Values of i and the output |
Part B: Predict the Output (4 min)
lab9_predict.cpp #include <iostream> using namespace std;
int main() { for (int i = 10; i > 0; i -= 2) { cout << i << " "; } cout << endl;
int total = 0; for (int k = 1; k <= 4; k++) { total = total + k * k; } cout << "total = " << total << endl;
for (int j = 5; j < 5; j++) { cout << "never" << endl; } cout << "done" << endl; return 0; } |
My prediction |
Part C: Bug Hunt (5 min)
bug1.cpp — wrong start value
#include <iostream> using namespace std;
int main() { int n = 5; long long power = 0; // 2 to the power n for (int i = 1; i <= n; i++) { power = power * 2; } cout << "2^" << n << " = " << power << endl; return 0; } |
Wrong output, cause and fix |
bug2.cpp — stray semicolon
#include <iostream> using namespace std;
int main() { int sum = 0; for (int i = 1; i <= 10; i++); { sum = sum + 5; } cout << "Ten fives make " << sum << endl; return 0; } |
Wrong output, cause and fix |
bug3.cpp — off by one
#include <iostream> using namespace std;
int main() { // print the days of a 30-day month: 1 to 30 int count = 0; for (int day = 1; day < 30; day++) { count++; } cout << "Days printed: " << count << endl; return 0; } |
Wrong output, cause and fix |
Part D: Graded Lab Tasks (32 min · 10 marks)
Rules Use for loops for counted repetition and do-while for validation. Use setw and fixed/setprecision for neat output. Save as lab9_task1.cpp, lab9_task2.cpp, lab9_task3.cpp. |
Task 1: Neat Multiplication Table (Easy — 3 marks)
Ask for a number from 1 to 20 (validate with do-while). Print its table from × 1 to × 12 with the columns lined up using setw.
Sample run Table of (1-20): 25 Table of (1-20): 7 7 x 1 = 7 7 x 2 = 14 … 7 x 12 = 84 |
Task 2: Class Result Summary (Medium — 3 marks)
Ask for the number of students (must be more than 0). Use a for loop to read each student's marks; each mark must be 0–100 (validate with a do-while inside the for loop). Print the highest, the lowest, the average (2 decimals) and how many passed (50 or more).
Sample run Number of students: 0 Number of students: 4 Marks of student 1 (0-100): 78 Marks of student 2 (0-100): 105 Marks of student 2 (0-100): 45 Marks of student 3 (0-100): 92 Marks of student 4 (0-100): 60 Highest : 92 Lowest : 45 Average : 68.75 Passed : 3 of 4 |
Task 3: Monthly Savings Plan (Challenge — 4 marks)
Input a monthly deposit and the number of months. At the start of each month the deposit is added; at the end of the month 1% profit (example rate) is added on the whole balance. Print a table of month, profit and balance, then the total deposited, total profit and final balance (2 decimals).
Sample run Monthly deposit (Rs.): 5000 Number of months: 12 Month Profit Balance 1 50.00 5050.00 2 100.50 10150.50 3 151.50 15302.00 … 12 634.13 64046.64 Total deposited: Rs. 60000.00 Total profit : Rs. 4046.64 Final balance : Rs. 64046.64 |
Part E: Post-Lab Questions (finish as homework if needed)
Q1. Why does a product (like 2 to the power n) start at 1 but a sum starts at 0?
Answer |
Q2. In Task 2, why is the do-while placed inside the for loop?
Answer |
Q3. Rewrite the loop of Task 1 as a while loop.
Answer |
Marking Rubric (10 marks)
Component | Marks | What earns full marks |
|---|---|---|
Task 1 | 3 | Validation; for 1–12; aligned columns. |
Task 2 | 3 | Validated n and marks; correct highest, lowest, average and pass count. |
Task 3 | 4 | Correct monthly order; aligned table; correct totals. |
Parts A–C and the post-lab questions are checked in the viva. A program that does not compile gets at most half marks for that task.
Submission
- Show your running programs to the instructor before leaving.
- Copy the final code and outputs into your Lab Book (Practical 9 pages).
- Copying code from a classmate gets zero marks for both students.
Next practical: Practical 10 — Nested Loops & Patterns (Week 6). |