2-D Arrays & Matrices in C
A 2-D array is essentially an array of arrays. It organizes data into a tabular grid of Rows and Columns, widely used for image processing, grids, tables, and mathematical matrices.
int mat[3][4];?Row × Column Grid
Indexed via arr[row][col]. Row index 0 to R-1, Col index 0 to C-1.
Row-Major Storage
In RAM, entire Row 0 is stored first, followed immediately by Row 1, then Row 2.
Nested Loops
Outer loop iterates rows (i), inner loop iterates columns (j).
Address Formula
$ ext{Addr} = ext{Base} + (i imes C + j) imes ext{sizeof(type)}$.
Row-Major Memory Order & Address Formula
Computer RAM is 1-dimensional. C maps 2D grid coordinates (i, j) into linear memory using the Row-Major Formula. Click any cell to inspect!
Interactive Row-Major Calculator
matrix[0][0] | Offset: k = 0 | RAM Address: 1000
2D Logical Matrix Grid (Click Any Cell)
Click any coordinate (i, j) to highlight its mapped position in flat RAM:
Matrix Traversal & Step-by-Step Tracer
Watch outer loop i hold constant while inner loop j iterates across every column of that row.
Interactive 2D Matrix Controls
i = 0 | Inner Loop: j = 0 | Action: Ready
Matrix Addition (C[i][j] = A[i][j] + B[i][j])
Two matrices can be added only if they have identical dimensions (M × N). Addition is performed element-by-element.
Matrix Addition Simulator
Step through each coordinate (i, j) and watch A[i][j] + B[i][j] compute into C[i][j]:
i = 0, j = 0C[0][0] = A[0][0] (1) + B[0][0] (9) = 10
Live Matrix Addition: A + B = C
Matrix Transpose (AT)
The transpose of matrix A is formed by flipping its rows over its columns: B[j][i] = A[i][j]. An M × N matrix becomes N × M.
Transpose Simulator
Watch each row in matrix A turn into a column in matrix B:
A[0][0] = 10 ➔
Dest: B[0][0]
Original A (2 × 3) ➔ Transposed AT (3 × 2)
AT (3×2)O(N³))Matrix Multiplication (C = A × B)
Matrix multiplication requires 3 nested loops: Row i of A dot-products with Column j of B across shared dimension k.
3-Nested-Loop Dot Product Visualizer
C[0][0] | Multiplier: k = 0A[0][0] × B[0][0]
Symmetric Matrices & Principal Diagonal Trace
A square matrix is Symmetric if A = AT (A[i][j] == A[j][i]). The Trace is the sum of main diagonal elements Sum of A[i][i].
Matrix Properties Inspector
[4, 5, 6] | Trace = 15Status: Symmetric Matrix (A == AT)
Diagonal (🟡 Gold) & Transpose Pairs
(i == j) are identical in a symmetric matrix!
Cache Locality: Row-Major vs Column-Major
Traversing memory sequentially utilizes CPU cache lines with spatial locality (⚡ Hits). Non-sequential stride causes CPU cache stalls (🛑 Misses)!
CPU L1 Cache Simulator
Select pattern and click "Step Access ▶" to inspect CPU cache behavior.
Common 2-D Array Pitfalls & Bugs
Avoid the top compilation errors and runtime memory bugs encountered when working with multi-dimensional arrays.
1. Index Inversion Bug [j][i]
Confusing row index with column index leads to transpose mistakes or immediate segmentation fault.
2. Omitting Column Dimension in Parameters
C compiler MUST know column width to calculate $(i imes C + j)$. Column width is mandatory!
3. Incompatible Matrix Multiplication
Multiplication of (R1 × C1) with (R2 × C2) is valid ONLY if C1 == R2.
4. Large Stack Allocation Overflow
Declaring huge 2D arrays on the stack will instantly trigger a stack overflow.