UNIVERSITY OF AGRICULTURE, FAISALABAD
Department of Computer Science
LECTURE 22 · STUDENT NOTES
2D Array Applications: Matrices, Tables & Grids
CS-307 Programming Fundamentals · BSCS Semester 1
Week | Slides | Linked practical | Reference ID |
9 | Lecture22_2D_Array_Applications.pptx | Practical 15 | N22 |
Learning Outcomes
SLO | By the end of this lecture you can… | Builds CLO | Bloom level |
|---|---|---|---|
SLO 22.1 | Compute row totals, column totals and averages of a 2D table | CLO4: Describe the syntax and semantics of the C++ programming language. | C3 |
SLO 22.2 | Write matrix programs for addition and transpose | CLO4: Describe the syntax and semantics of the C++ programming language. | C3 |
SLO 22.3 | 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. | C4 |
How to use these notes Read these notes after the lecture and before the practical. Section numbers (for example §3.2) are used in the question bank, so you can always find where an answer comes from. Type every code example yourself — do not copy-paste. |
Lecture 21 built 2D arrays. This lecture uses them for three kinds of real work: tables (totals and averages), matrices (addition and transpose) and grids (a bus seat map).
1. Tables: Row Totals and Averages
For each row (student), set rowTotal = 0, add the row's elements in the inner loop and print the total after the inner loop. This is the sum pattern run once per row. (Slide L22-S05)
Figure 22.1 – A column total runs down the rows; the column loop is outside (Slide L22-S06)
row_col.cpp #include <iostream> #include <iomanip> using namespace std;
int main() { const int STU = 4, SUB = 3; int marks[STU][SUB] = {{72, 85, 64}, {58, 77, 81}, {91, 66, 74}, {45, 70, 59}};
cout << "Student PF Cal Eng Total" << endl; for (int r = 0; r < STU; r++) { int rowTotal = 0; // reset for every row cout << setw(7) << r + 1; for (int c = 0; c < SUB; c++) { cout << setw(5) << marks[r][c]; rowTotal += marks[r][c]; } cout << setw(7) << rowTotal << endl; }
cout << fixed << setprecision(1) << "Average"; for (int c = 0; c < SUB; c++) { // columns outside int colTotal = 0; for (int r = 0; r < STU; r++) // rows inside colTotal += marks[r][c]; cout << setw(5) << (double) colTotal / STU; } cout << endl; return 0; } |
Output 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 Average 66.5 74.5 69.5 |
1.1 Column totals: swap the loops
To total a column, fix c and run r down the rows, so the column loop goes outside. A column average divides by the number of rows. (Slide L22-S06)
Want | Outer loop | Inner loop | Divide the average by |
|---|---|---|---|
Row totals | rows | columns | COLS |
Column totals | columns | rows | ROWS |
1.2 The reset bug
(Slide L22-S07)
rowtotal_bug.cpp #include <iostream> using namespace std;
int main() { int m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int rowTotal = 0; // BUG: should be inside the row loop for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) rowTotal += m[r][c]; cout << "Row " << r << " total: " << rowTotal << endl; } return 0; } |
Output — wrong (expected 6, 15, 24) Row 0 total: 6 Row 1 total: 21 Row 2 total: 45 |
rowTotal is set to 0 only once, so it keeps growing. Fix: declare int rowTotal = 0; as the first line inside the outer loop.
2. Matrices
A matrix is a rectangular table of numbers used in maths, statistics and image processing (a photo is a matrix of pixel values).
2.1 Matrix addition
Two matrices of the same size are added position by position: S[r][c] = A[r][c] + B[r][c]. (Slide L22-S08)
matrix_add.cpp #include <iostream> using namespace std;
int main() { const int R = 2, C = 3; 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]; // same position in both
for (int r = 0; r < R; r++) { for (int c = 0; c < C; c++) cout << S[r][c] << " "; cout << endl; } return 0; } |
Output 11 22 33 44 55 66 |
2.2 Transpose
The transpose swaps rows and columns: T[c][r] = A[r][c]. A 2 × 3 matrix becomes 3 × 2, so the result array is declared T[C][R]. (Slide L22-S09)
Figure 22.2 – Transpose: row 0 of A becomes column 0 of T (Slide L22-S09)
transpose.cpp #include <iostream> using namespace std;
int main() { const int R = 2, C = 3; int A[R][C] = {{1, 2, 3}, {4, 5, 6}}; int T[C][R]; // 3 rows, 2 columns
for (int r = 0; r < R; r++) for (int c = 0; c < C; c++) T[c][r] = A[r][c]; // row becomes column
for (int r = 0; r < C; r++) { for (int c = 0; c < R; c++) cout << T[r][c] << " "; cout << endl; } return 0; } |
Output 1 4 2 5 3 6 |
3. A Real Grid: Bus Seat Map
A coach section has 5 rows and 4 seats (A–D). We plan with top-down design (Lecture 7) before coding. (Slides L22-S10, S11)
Figure 22.3 – Design of the seat map: data first, then operations (Slide L22-S10)
Step | In C++ |
|---|---|
1 Data | char seat[5][4]; '.' = free, 'X' = booked |
2 Start | nested loop sets every seat to '.' |
3 Book (repeat until 0 0) | read r s; check the range; check it is free; mark 'X' |
4 Show | nested loop prints the map |
5 Count | booked; free = 20 − booked |
seats.cpp #include <iostream> using namespace std;
int main() { const int ROWS = 5, SEATS = 4; char seat[ROWS][SEATS]; for (int r = 0; r < ROWS; r++) for (int s = 0; s < SEATS; s++) seat[r][s] = '.'; // '.' = free
int r, s, booked = 0; cout << "Book row (1-5) and seat (1-4), 0 0 to stop: "; cin >> r >> s; 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; }
cout << " A B C D" << endl; for (int i = 0; i < ROWS; i++) { cout << "Row " << i + 1; for (int j = 0; j < SEATS; j++) cout << " " << seat[i][j]; cout << endl; } cout << "Booked: " << booked << ", free: " << ROWS * SEATS - booked << endl; return 0; } |
Session Book row (1-5) and seat (1-4), 0 0 to stop: 1 1 Booked. Book row (1-5) and seat (1-4), 0 0 to stop: 1 2 Booked. Book row (1-5) and seat (1-4), 0 0 to stop: 3 4 Booked. Book row (1-5) and seat (1-4), 0 0 to stop: 1 2 Already booked. Book row (1-5) and seat (1-4), 0 0 to stop: 6 1 No such seat. Book row (1-5) and seat (1-4), 0 0 to stop: 5 4 Booked. Book row (1-5) and seat (1-4), 0 0 to stop: 0 0 A B C D Row 1 X X . . Row 2 . . . . Row 3 . . . X Row 4 . . . . Row 5 . . . X Booked: 4, free: 16 |
- People say 'row 1, seat 1' but the array uses seat[0][0], so we use seat[r - 1][s - 1].
- Check the range first, then use the index — otherwise row 6 would read outside the array.
4. Common Mistakes
Mistake | Result | Fix |
|---|---|---|
Total set to 0 once, outside the outer loop | running totals (6, 21, 45) | reset inside the outer loop |
Column average divided by COLS | wrong averages | divide by ROWS |
Adding matrices of different sizes | meaningless / out of bounds | check sizes match |
Transpose stored in T[R][C] | wrong shape, out of bounds | declare T[C][R] |
Using seat[r][s] before the range check | out of bounds for bad input | check 1 ≤ r ≤ ROWS first |
5. Word Bank (Key Terms)
Word | Simple meaning |
|---|---|
Matrix | A rectangular table of numbers |
Transpose | Swap rows and columns |
Row total | Sum of one row |
Column total | Sum of one column |
Grid | A 2D layout of places, e.g. seats or a game board |
6. Self-Check Questions
1. For m = {{2, 7, 6}, {9, 5, 1}, {4, 3, 8}}, what is the total of column 1?
2. What is the first row of the transpose of m?
3. m + B where every element of B is 1: the first row?
4. In an empty 5 × 4 seat map, (2,3), (2,4) and (1,1) are booked. How many seats are free?
5. Where must rowTotal be set to 0?
6. What is the size of the transpose of a 2 × 3 matrix?
Answers: 15; 2 9 4; 3 8 7; 17; inside the outer loop (§1.2); 3 × 2 (§2.2). Homework is on Slide L22-S14.