PSUC Lab 8 — Matrix Grid Operations
Active Logic: 2-Dimensional Arrays (Matrices)
Master 2D row-column indexing (mat[row][col]), student marks row/col accumulators, matrix searching, matrix multiplication, symmetry, trace, and norm.
2-D Matrix Rules
| Concept | Rule / Formula | Key Trap / Pitfall |
|---|---|---|
| Indexing | mat[i][j] (i=row, j=col) | Do not confuse row index i with column index j! |
| Multiplication | C[i][j] = ∑ (A[i][k] × B[k][j]) | Requires 3 nested loops! (i, j, k) |
| Symmetry | A[i][j] == A[j][i] | Matrix must be square (Rows = Cols) |
| Trace & Norm | Trace = ∑ A[i][i], Norm = √∑(A[i][j]2) | Trace is diagonal sum, Norm is Frobenius norm |
🎯 Learning Philosophy: Active Logic Building
Test your 2-D matrix logic across 3 Interactive Learning Modes:
- 🧠 Tab 1 (Logic & Hints): Matrix indices & multiplication rules.
- 🧩 Tab 2 (Logic Puzzle): Complete missing row/col loop bounds & matrix visualizers!
- 💻 Tab 3 (C Solution): Reference verified C code solutions.
Solve matrix puzzles before peeking at C source code!
Exercise 1 of 5
Student Marks Totals & Subject Averages
Computes row totals (total marks per student) and column averages (average marks per subject) in a 2D marks matrix.
Row vs Column Sum
- Row Sum (Student Total): Outer loop
i, inner loopj→sum += marks[i][j] - Column Sum (Subject Avg): Outer loop
j, inner loopi→sum += marks[i][j]
Interactive Logic Hints
💡 Hint 1: Where to reset sum = 0? ▼
Reset
sum = 0 INSIDE the outer loop before starting the inner loop for each row/column!🧩 Complete Marks Accumulator Loops
// Student Total (Row Sum)
for (i = 0; i < 3; i++) {
studentTotal = 0;
for (j = 0; j < 3; j++) studentTotal += marks[][];
}
// Subject Average (Column Sum)
for (j = 0; j < 3; j++) {
subSum = 0;
for (i = 0; i < 3; i++) subSum += marks[][j];
}
for (i = 0; i < 3; i++) {
studentTotal = 0;
for (j = 0; j < 3; j++) studentTotal += marks[][];
}
// Subject Average (Column Sum)
for (j = 0; j < 3; j++) {
subSum = 0;
for (i = 0; i < 3; i++) subSum += marks[][j];
}
Select matrix indices above and click Check!
📊 Marks Table Calculator
Matrix output...
Totals & Averages...
student_marks.cReference C Code
#include <stdio.h>
int main() {
int marks[3][3] = {{85, 90, 78}, {72, 88, 95}, {91, 65, 80}};
int i, j, studentTotal;
for (i = 0; i < 3; i++) {
studentTotal = 0;
for (j = 0; j < 3; j++) studentTotal += marks[i][j];
printf("Student %d Total = %d\n", i + 1, studentTotal);
}
for (j = 0; j < 3; j++) {
int subSum = 0;
for (i = 0; i < 3; i++) subSum += marks[i][j];
printf("Subject %d Avg = %.2f\n", j + 1, subSum / 3.0);
}
return 0;
}
Exercise 2 of 5
Search Element in 2-D Matrix
Searches a target key in 2-D matrix and returns its exact [row][col] position.
Matrix Search Traversal
- Outer loop
i = 0 to ROWS-1 - Inner loop
j = 0 to COLS-1 - If
mat[i][j] == key→found = true; row = i; col = j; break;
Interactive Logic Hints
💡 Hint 1: What is the time complexity of matrix search? ▼
Linear matrix search takes O(Rows × Cols) operations!
🧩 Complete Matrix Search Loop
bool found = false;
for (i = 0; i < R; i++) {
for (j = 0; j < C; j++) {
if (mat[i][j] key) {
found = true; ;
}
}
}
for (i = 0; i < R; i++) {
for (j = 0; j < C; j++) {
if (mat[i][j] key) {
found = true; ;
}
}
}
Select statements above and click Check!
🔍 Matrix Key Search Engine
Matrix grid...
Search result...
matrix_search.cReference C Code
#include <stdio.h>
#include <stdbool.h>
int main() {
int mat[3][3] = {{10, 20, 30}, {40, 95, 60}, {70, 80, 90}};
int key = 95, i, j, foundRow = -1, foundCol = -1;
bool found = false;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (mat[i][j] == key) {
found = true; foundRow = i; foundCol = j; break;
}
}
if (found) break;
}
if (found) printf("Key %d found at row %d, col %d\n", key, foundRow, foundCol);
else printf("Key %d NOT found.\n", key);
return 0;
}
Exercise 3 of 5
Matrix Multiplication (C = A × B)
Multiplies two matrices Ar1 × c1 and Br2 × c2 (Requires c1 == r2).
3-Loop Multiplication Logic
- Outer
i: Row of matrix A - Middle
j: Column of matrix B - Inner
k: Dot product accumulator →C[i][j] += A[i][k] * B[k][j]
Interactive Logic Hints
💡 Hint 1: What is the condition for matrix multiplication? ▼
Number of columns in A MUST equal number of rows in B! (
c1 == r2).🧩 Complete Matrix Multiplication Dot Product
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
C[i][j] = 0;
for (k = 0; k < c1; k++) {
C[i][j] += A[i][] * B[][j];
}
}
}
for (j = 0; j < c2; j++) {
C[i][j] = 0;
for (k = 0; k < c1; k++) {
C[i][j] += A[i][] * B[][j];
}
}
}
Select inner dot product indices and click Check!
✖️ Matrix Multiplication Calculator
Matrix A & B product...
Result matrix...
matrix_mult.cReference C Code
#include <stdio.h>
int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int C[2][2], i, j, k;
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
C[i][j] = 0;
for (k = 0; k < 2; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
printf("Product Matrix C:\n%d %d\n%d %d\n", C[0][0], C[0][1], C[1][0], C[1][1]);
return 0;
}
Exercise 4 of 5
Matrix Symmetry & Magic Square Verification
Verifies if a matrix is Symmetric (A[i][j] == A[j][i]) and if it forms a Magic Square (equal row, column, and diagonal sums).
Symmetry & Magic Rules
- Symmetry:
arr[i][j] == arr[j][i]for alli, j - Magic Square: All row sums = All col sums = Main diag sum = Anti diag sum
Interactive Logic Hints
💡 Hint 1: What is a famous 3x3 Magic Square sum? ▼
The standard 3x3 magic square sum using numbers 1-9 is 15!
🧩 Complete Matrix Symmetry Check
bool isSymmetric = true;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (mat[i][j] mat[][]) {
isSymmetric = false; break;
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (mat[i][j] mat[][]) {
isSymmetric = false; break;
}
}
}
Select statements above and click Check!
🔮 Magic Square & Symmetry Tester
3x3 Test Matrix...
Symmetry & Magic status...
matrix_magic.cReference C Code
#include <stdio.h>
#include <stdbool.h>
int main() {
int mat[3][3] = {{8, 1, 6}, {3, 5, 7}, {4, 9, 2}};
int i, j;
bool isSymmetric = true;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (mat[i][j] != mat[j][i]) { isSymmetric = false; break; }
}
}
printf("Symmetric: %s\n", isSymmetric ? "YES" : "NO");
return 0;
}
Exercise 5 of 5
Matrix Trace & Frobenius Norm
Computes Matrix Trace (Trace = ∑ A[i][i]) and Frobenius Norm (Norm = √∑(A[i][j]2)).
Trace & Norm Equations
- Trace: Sum of main diagonal elements:
trace += mat[i][i] - Norm: Square root of sum of squares:
norm += mat[i][j] * mat[i][j], thensqrt(norm)
Interactive Logic Hints
💡 Hint 1: What header is needed for sqrt()? ▼
Include
#include <math.h> and link math library (-lm)!🧩 Complete Trace & Norm Logic
for (i = 0; i < n; i++) {
trace += mat[][];
for (j = 0; j < n; j++) {
normSum += mat[i][j] * mat[i][j];
}
}
double norm = (normSum);
trace += mat[][];
for (j = 0; j < n; j++) {
normSum += mat[i][j] * mat[i][j];
}
}
double norm = (normSum);
Select functions above and click Check!
📐 Matrix Trace & Norm Calculator
3x3 Matrix...
Trace & Norm values...
matrix_trace_norm.cReference C Code
#include <stdio.h>
#include <math.h>
int main() {
int mat[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int i, j, trace = 0;
double normSum = 0.0, norm;
for (i = 0; i < 3; i++) {
trace += mat[i][i];
for (j = 0; j < 3; j++) {
normSum += mat[i][j] * mat[i][j];
}
}
norm = sqrt(normSum);
printf("Trace = %d\nNorm = %.2f\n", trace, norm);
return 0;
}
Knowledge Check
Test Your 2-D Matrix Logic Mastery
Question 1 of 5
Loading question...