Duration | Language | Tools | Linked Lecture |
|---|---|---|---|
50 minutes | C++ | Dev-C++ or Code::Blocks (online compiler as backup) | Lectures 21 and 22 |
Mapped outcomes: CLO4 (C++ syntax and semantics) at psychomotor level P3 – Guided Response · PLO2, PLO5
1. Lab Objectives
By the end of this 50-minute lab, you will be able to:
- Declare, read and print 2D arrays with nested loops.
- Compute row totals, column totals, averages and diagonal sums.
- Model a real grid (bus seats) with a 2D char array and a menu.
- Find the too-many-values, total-not-reset and wrong-divisor bugs.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
2D array | A table: rows × columns | A result sheet |
a[r][c] | Row r, column c | Seat row 3, seat B |
Main diagonal | a[0][0], a[1][1], a[2][2] … | The cells from top-left to bottom-right |
Row total | Sum of one row | A student's total marks |
Column total | Sum of one column | Total marks in one subject |
3. Background
Reminder int m[ROWS][COLS]; for (int r = 0; r < ROWS; r++) { // row totals: rows outside int rowTotal = 0; // reset for every row for (int c = 0; c < COLS; c++) rowTotal += m[r][c]; } for (int c = 0; c < COLS; c++) { // column totals: columns outside int colTotal = 0; for (int r = 0; r < ROWS; r++) colTotal += m[r][c]; } |
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)
lab15_warmup.cpp #include <iostream> using namespace std;
int main() { int g[2][3] = {{1, 2, 3}, {4, 5, 6}}; g[1][0] += g[0][2]; g[0][1] = g[1][1] * 2; g[1][2] = g[0][0] + g[1][0]; for (int r = 0; r < 2; r++) { for (int c = 0; c < 3; c++) cout << g[r][c] << " "; cout << endl; } return 0; } |
Draw the 2 × 3 grid after each assignment and write the output |
Part B: Predict the Output (4 min)
lab15_predict.cpp #include <iostream> using namespace std;
int main() { int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) cout << a[c][r] << " "; cout << endl; } int s = 0; for (int i = 0; i < 3; i++) s += a[i][2 - i]; cout << "s = " << s << endl; int even = 0; for (int r = 0; r < 3; r++) for (int c = 0; c < 3; c++) if (a[r][c] % 2 == 0) even++; cout << "even = " << even << endl; return 0; } |
My prediction |
Part C: Bug Hunt (5 min)
bug1.cpp — will not compile
#include <iostream> using namespace std;
int main() { int t[2][3] = {{1, 2, 3, 4}, {5, 6, 7}}; cout << t[0][0] << endl; return 0; } |
Error or wrong output, cause and fix |
bug2.cpp — wrong house totals
#include <iostream> using namespace std;
int main() { int bill[2][3] = {{1500, 1800, 1650}, {2200, 2100, 2400}}; // 2 houses x 3 months int total = 0; for (int h = 0; h < 2; h++) { for (int m = 0; m < 3; m++) total += bill[h][m]; cout << "House " << h + 1 << " total: " << total << endl; } return 0; } |
Error or wrong output, cause and fix |
bug3.cpp — wrong column averages
#include <iostream> using namespace std;
int main() { const int ROWS = 4, COLS = 3; int m[ROWS][COLS] = {{60, 70, 80}, {50, 90, 70}, {80, 60, 90}, {70, 80, 60}}; for (int c = 0; c < COLS; c++) { int total = 0; for (int r = 0; r < ROWS; r++) total += m[r][c]; cout << "Column " << c << " average: " << total / (double) COLS << endl; } return 0; } |
Error or wrong output, cause and fix |
Part D: Graded Lab Tasks (32 min · 10 marks)
Rules Use named constants for the number of rows and columns. Reset totals inside the outer loop. Save as lab15_task1.cpp, lab15_task2.cpp, lab15_task3.cpp. |
Task 1: Matrix With Row Sums and Diagonal (Easy — 3 marks)
Read a 3 × 3 matrix one row at a time. Print it as a grid with each row's sum after a |, then print the sum of the main diagonal.
Sample run Row 1 (3 numbers): 2 7 6 Row 2 (3 numbers): 9 5 1 Row 3 (3 numbers): 4 3 8 Matrix and row sums: 2 7 6 | 15 9 5 1 | 15 4 3 8 | 15 Main diagonal sum: 15 |
Task 2: Result Table (Medium — 3 marks)
Read the marks of 4 students in PF, Calculus and English (each 0–100; ask again if invalid). Print a table with each student's marks, total and average (1 decimal), then the average of each subject and the student with the highest total.
Sample run Marks of student 1 in PF, Calculus, English: 72 85 64 Marks of student 2 in PF, Calculus, English: 58 77 181 0-100 only, enter again: 81 Marks of student 3 in PF, Calculus, English: 91 66 74 Marks of student 4 in PF, Calculus, English: 45 70 59 Student PF Cal Eng Total Avg 1 72 85 64 221 73.7 2 58 77 81 216 72.0 3 91 66 74 231 77.0 4 45 70 59 174 58.0 Subject average 66.5 74.5 69.5 Top student: 3 (total 231) |
Task 3: Coach Seat Booking Menu (Challenge — 4 marks)
A coach has 6 rows × 4 seats, all free ('.') at the start. Repeat a menu until 0: 1 book a seat, 2 cancel a booking, 3 show the map, 0 exit. Refuse seats that do not exist, booking a booked seat and cancelling a free seat. On exit print the number of booked and free seats and the fullest row.
Sample run 1 Book 2 Cancel 3 Show map 0 Exit: 1 Row (1-6) and seat (1-4): 2 3 Booked. 1 Book 2 Cancel 3 Show map 0 Exit: 1 Row (1-6) and seat (1-4): 2 4 Booked. 1 Book 2 Cancel 3 Show map 0 Exit: 1 Row (1-6) and seat (1-4): 2 3 Already booked. 1 Book 2 Cancel 3 Show map 0 Exit: 1 Row (1-6) and seat (1-4): 5 1 Booked. 1 Book 2 Cancel 3 Show map 0 Exit: 2 Row (1-6) and seat (1-4): 5 1 Cancelled. 1 Book 2 Cancel 3 Show map 0 Exit: 2 Row (1-6) and seat (1-4): 4 4 That seat is not booked. 1 Book 2 Cancel 3 Show map 0 Exit: 1 Row (1-6) and seat (1-4): 7 1 No such seat. 1 Book 2 Cancel 3 Show map 0 Exit: 9 Invalid choice. 1 Book 2 Cancel 3 Show map 0 Exit: 3 A B C D Row 1 . . . . Row 2 . . X X Row 3 . . . . Row 4 . . . . Row 5 . . . . Row 6 . . . . 1 Book 2 Cancel 3 Show map 0 Exit: 0 Booked: 2, free: 22 Fullest row: 2 (2 booked) |
Part E: Post-Lab Questions (finish as homework if needed)
Q1. In Task 2, why does the subject-average loop have the column loop outside?
Answer |
Q2. In Task 3, why must the range be checked before seat[r − 1][s − 1] is used?
Answer |
Q3. How many bytes does char seat[6][4] use, and how many bytes after seat[0][0] does seat[2][3] start?
Answer |
Marking Rubric (10 marks)
Component | Marks | What earns full marks |
|---|---|---|
Task 1 | 3 | Matrix input; grid with row sums; diagonal sum. |
Task 2 | 3 | Validated table; row totals and averages; subject averages; top student. |
Task 3 | 4 | Menu loop; all checks in the right order; map; booked, free and fullest row. |
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 15 pages).
- Copying code from a classmate gets zero marks for both students.
Next practical: Practical 16 — User-Defined Functions (Week 10). |