Duration | Language | Tools | Linked Lecture |
|---|---|---|---|
50 minutes | C++ | Dev-C++ or Code::Blocks (online compiler as backup) | Lecture 15 (and 13–14) |
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 a nested loop and count how many times its inner body runs.
- Print rectangles, grids and triangles with nested loops.
- Design a pattern with the five-step method (rows → items per row → rule → what to print → endl).
- Find the endl-in-the-wrong-loop, wrong-limit and counter-not-reset bugs.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Nested loop | A loop inside another loop | Hours and minutes on a clock |
Outer loop | Controls the rows (lines) | Rows of seats in an exam hall |
Inner loop | Controls the items in one row | Seats in one row |
Pattern | A shape printed row by row | Tiles on a floor |
Header row | A first line that labels the columns | Days of the week at the top of a timetable |
3. Background
Reminder for (int i = 1; i <= rows; i++) { // outer: rows for (int j = 1; j <= LIMIT; j++) { // inner: items in row i cout << WHAT; } cout << endl; // after the inner loop } |
Pattern | Inner limit |
|---|---|
Rectangle | j <= cols |
Triangle | j <= i |
Inverted triangle | j <= n - i + 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)
lab10_warmup.cpp #include <iostream> using namespace std;
int main() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 2; j++) { cout << i << j << " "; } cout << endl; } return 0; } |
Output, and how many times the inner body runs |
Part B: Predict the Output (4 min)
lab10_predict.cpp #include <iostream> using namespace std;
int main() { for (int i = 1; i <= 4; i++) { for (int j = 1; j <= i; j++) { cout << i; } cout << endl; } for (int i = 3; i >= 1; i--) { for (int j = 1; j <= i; j++) { cout << "#"; } cout << endl; } return 0; } |
My prediction |
Part C: Bug Hunt (5 min)
bug1.cpp — endl in the wrong loop
#include <iostream> using namespace std;
// Should print a 2 x 4 rectangle of stars int main() { for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 4; j++) { cout << "*" << endl; } } return 0; } |
Wrong output, cause and fix |
bug2.cpp — wrong inner limit
#include <iostream> using namespace std;
// Should print a triangle: 1, 2, 3, 4 stars int main() { for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { cout << "*"; } cout << endl; } return 0; } |
Wrong output, cause and fix |
bug3.cpp — inner counter not reset
#include <iostream> using namespace std;
// Should print a triangle: 1, 2, 3, 4 stars int main() { int j = 1; for (int i = 1; i <= 4; i++) { while (j <= i) { cout << "*"; j++; } cout << endl; } return 0; } |
Wrong output, cause and fix |
Part D: Graded Lab Tasks (32 min · 10 marks)
Rules Use nested for loops; validate inputs with do-while. Write the items-per-row table before coding Task 3. Save as lab10_task1.cpp, lab10_task2.cpp, lab10_task3.cpp. |
Task 1: Character Rectangle (Easy — 3 marks)
Ask for the number of rows and columns (each 1–20, validated) and a character. Print a rectangle of that character, then the number of characters printed.
Sample run Rows (1-20): 0 Rows (1-20): 3 Columns (1-20): 25 Columns (1-20): 5 Character: # ##### ##### ##### Characters printed: 15 |
Task 2: Multiplication Grid With Labels (Medium — 3 marks)
Ask for a grid size n (2–10). Print an n × n multiplication grid with a header row of column numbers and a row number at the start of each row. Use setw(4) so the columns line up.
Sample run Grid size (2-10): 1 Grid size (2-10): 4 x 1 2 3 4 1 1 2 3 4 2 2 4 6 8 3 3 6 9 12 4 4 8 12 16 |
Task 3: Number Pyramid (Challenge — 4 marks)
Ask for the number of rows n (1–9). Print a centred number pyramid: row i has n − i spaces, then the numbers 1 up to i, then back down to 1.
Sample run Rows (1-9): 12 Rows (1-9): 4 1 121 12321 1234321 |
Row i (n = 4) | Spaces | Going up | Going down |
|---|---|---|---|
1 | 3 | 1 | — |
2 | 2 | 1 2 | 1 |
3 | 1 | 1 2 3 | 2 1 |
4 | 0 | 1 2 3 4 | 3 2 1 |
Part E: Post-Lab Questions (finish as homework if needed)
Q1. An outer loop runs 6 times and the inner loop 4 times. How many times does the inner body run?
Answer |
Q2. In Task 3, how many characters (spaces + digits) are printed on row i, before endl?
Answer |
Q3. Why must the inner loop counter start again in every round of the outer loop?
Answer |
Marking Rubric (10 marks)
Component | Marks | What earns full marks |
|---|---|---|
Task 1 | 3 | Validated inputs; correct rectangle; correct count. |
Task 2 | 3 | Header row, row labels and aligned products. |
Task 3 | 4 | Correct spaces, up and down loops; centred pyramid. |
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 10 pages).
- Copying code from a classmate gets zero marks for both students.
Next practical: Practical 11 — Loop-Based Problem Solving (Week 6). |