Slide 2
Quick Recap of Lecture 21
4 minutes · homework check
- Quick-fire
- Storage order? row-major
- Outer loop when printing? rows
- Offset of a[r][c]? (r × COLS + c) × size
- Today
- Three uses of 2D arrays: TABLES (totals and averages), MATRICES (addition, transpose) and GRIDS (a bus seat map).
Recap
Programming Fundamentals · Lecture 22
Slide 3
Today's Plan
50 minutes · 10 parts
Recap & homework check
4 min
Learning outcomes
2 min
Row totals & averages
7 min
Column totals (and the reset bug)
7 min
Matrix addition
6 min
Transpose
5 min
A real grid: bus seat map
9 min
Class activity
6 min
Recap
3 min
Homework & next lecture
1 min
Programming Fundamentals · Lecture 22
Slide 4
Learning Outcomes (SLOs)
By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)
Compute row totals, column totals and averages of a 2D table
SLO 22.1 → CLO4 · C3
Write matrix programs for addition and transpose
SLO 22.2 → CLO4 · C3
SLO 22.3 → CLO1 · C4
Model a real grid (a bus seat map) with a 2D array and design its processing
- CLO1 Identify the necessary properties of good problem-solving techniques.
- CLO4 Describe the syntax and semantics of the C++ programming language.
Programming Fundamentals · Lecture 22
Slide 5
Row Totals and Averages
One total per student: reset the total at the start of every row
row_col.cpp (row part)
Output
- for (int r = 0; r < STU; r++) {
- int rowTotal = 0; // reset!
- for (int c = 0; c < SUB; c++)
- rowTotal += marks[r][c];
- cout << rowTotal << endl;
- }
- Student PF Cal Eng Total
- 1 72 85 64 221
- 2 58 77 81 216
- 3 91 66 74 231
- 4 45 70 59 174
row 1 → 216
- Pattern
- Row total: rows OUTSIDE, columns inside, total reset inside the outer loop. Row average = rowTotal / (double) SUB.
Module 1 · Tables
Programming Fundamentals · Lecture 22
Slide 6
Column Totals: Swap the Loops
One total per subject: columns outside, rows inside
row_col.cpp (column part)
Output
- for (int c = 0; c < SUB; c++) { // cols
- int colTotal = 0;
- for (int r = 0; r < STU; r++) // rows
- colTotal += marks[r][c];
- cout << (double) colTotal / STU;
- }
Average 66.5 74.5 69.5
- Divide by
- the number of ROWS (4 students)
column 1 → 298
Module 1 · Tables
Programming Fundamentals · Lecture 22
Slide 7
The Reset Bug
A total declared once, outside the outer loop, keeps growing
rowtotal_bug.cpp (m = 1…9)
Output — wrong
- int rowTotal = 0; // BUG: once only
- for (int r = 0; r < 3; r++) {
- for (int c = 0; c < 3; c++)
- rowTotal += m[r][c];
- cout << "Row " << r << " total: " << rowTotal;
- }
- Row 0 total: 6
- Row 1 total: 21
- Row 2 total: 45
- Expected
- 6, 15, 24
- Fix
- Move int rowTotal = 0; to the first line inside the outer loop — a fresh total for every row.
Module 1 · Tables
Programming Fundamentals · Lecture 22
Slide 8
Matrix Addition
Add the elements in the same position: S[r][c] = A[r][c] + B[r][c]
+
=
A (2 × 3)
B (2 × 3)
S (2 × 3)
matrix_add.cpp (main part)
Output
- int A[R][C] = {{1, 2, 3}, {4, 5, 6}};
- int B[R][C] = {{10, 20, 30}, {40, 50, 60}};
- int S[R][C];
- for (int r = 0; r < R; r++)
- for (int c = 0; c < C; c++)
- S[r][c] = A[r][c] + B[r][c];
- 11 22 33
- 44 55 66
- Rule
- Both matrices must have the same rows and columns.
Module 2 · Matrices
Programming Fundamentals · Lecture 22
Slide 9
Transpose: Rows Become Columns
T[c][r] = A[r][c] · a 2 × 3 matrix becomes 3 × 2
transpose.cpp (main part)
→
- int T[C][R]; // sizes swapped
- for (int r = 0; r < R; r++)
- for (int c = 0; c < C; c++)
- T[c][r] = A[r][c];
A: 2 × 3
T: 3 × 2
- Check
- A[0][2] = 3 → T[2][0] = 3
- Row index ↔ column index
Output
- Sizes
- T is [C][R], not [R][C]
- 1 4
- 2 5
- 3 6
Module 2 · Matrices
Programming Fundamentals · Lecture 22
Slide 10
Designing a Bus Seat Map
Top-down design first: data, then operations
A
B
C
D
Row 1
X
X
.
.
Row 2
.
.
.
.
.
.
.
X
Row 3
Row 4
.
.
.
.
Row 5
.
.
.
X
- People count from 1
- Row 1, seat 1 is seat[0][0]: use seat[r − 1][s − 1] and check 1 ≤ r ≤ 5, 1 ≤ s ≤ 4 first.
Module 3 · A Real Grid
Programming Fundamentals · Lecture 22
Slide 11
Bus Seat Map in C++
Sentinel loop + two checks + a 2D char array
seats.cpp (main part)
Result
- while (r != 0) {
- if (r < 1 || r > ROWS || s < 1 || s > SEATS)
- cout << " No such seat." << endl;
- else if (seat[r - 1][s - 1] == 'X')
- cout << " Already booked." << endl;
- else {
- seat[r - 1][s - 1] = 'X';
- booked++;
- cout << " Booked." << endl;
- }
- cout << "Book row (1-5) and seat (1-4), 0 0 to stop: ";
- cin >> r >> s;
- }
- Input: 1 1, 1 2, 3 4, 1 2,
- 6 1, 5 4, 0 0
- A B C D
- Row 1 X X . .
- Row 2 . . . .
- Row 3 . . . X
- Row 4 . . . .
- Row 5 . . . X
- Booked: 4, free: 16
- Range check first
- then use the index
Module 3 · A Real Grid
Programming Fundamentals · Lecture 22
Slide 12
CLASS ACTIVITY · 6 MIN · IN PAIRS
Tables, Matrices, Grids
m = {{2, 7, 6}, {9, 5, 1}, {4, 3, 8}}. Work on paper, then check by running.
Questions
Hints
- 1. The total of column 1 of m?
- 2. The first row of the transpose of m?
- 3. m + B where every element of B is 1: the first row?
- 4. In the empty 5 × 4 seat map, (2,3), (2,4) and (1,1) are booked. How many seats are free?
- 1: 7 + 5 + 3
- 2: column 0 of m
- 3: add 1 to each
- 4: 20 − booked
Slide 13
Quick Recap
Answer aloud
Q1
Where must rowTotal be set to 0, and what goes wrong otherwise?
Q2
For column averages, which loop is outside and what do you divide by?
Q3
What size is the transpose of a 2 × 3 matrix, and what is T[2][0] if A[0][2] = 3?
Q4
In the seat map, why is the range checked before seat[r − 1][s − 1] is used?
Programming Fundamentals · Lecture 22
Slide 14
Homework
For m = {{2, 7, 6}, {9, 5, 1}, {4, 3, 8}} print both diagonal sums. Why is m called a magic square?
Multiply the 2 × 2 matrices A = {{1, 2}, {3, 4}} and B = {{5, 6}, {7, 8}} (row × column).
Read a family's expenses for 4 weeks × 5 categories (food, bills, travel, school, other); print weekly totals and category totals.
Extend seats.cpp: print the number of free seats in each row and the number of free window seats (A and D).
Next lecture: Introduction to modular programming (functions) — Week 10