Matrix addition adds matching cells: C[i][j] = A[i][j] + B[i][j]. Both matrices must have the same number of rows and columns.
Remember
A + B is defined only when A and B have the same shape
[ 1 2 ] [ 5 6 ] [ 6 8 ]
[ 3 4 ] + [ 7 8 ] = [10 12 ]
Rule: (A + B)[i][j] = A[i][j] + B[i][j]
Position
A
B
A + B
[0][0]
1
5
6
[0][1]
2
6
8
[1][0]
3
7
10
[1][1]
4
8
12
Approach
How to Add Matrices in C
Four clear steps — nested loops do the work cell by cell.
Confirm both matrices have the same rows and columns.
Create a result matrix of that same size.
Outer loop walks rows; inner loop walks columns.
Set result[i][j] = A[i][j] + B[i][j] for every cell.
Method
Idea
Time
Best for
Nested loops
Add matching cells into a new 2D array
O(m × n)
Interviews, teaching
Shape-safe add
Validate rows/cols first, then add
O(m × n)
Safer production-style code
Pseudocode
Pseudocode
function AddMatrices(A, B):
rows = rows of A
cols = cols of A
if rows != rows of B or cols != cols of B:
error "shapes must match"
C = new matrix[rows][cols]
for i from 0 to rows - 1:
for j from 0 to cols - 1:
C[i][j] = A[i][j] + B[i][j]
return C
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Rule
C[i][j] = A[i][j] + B[i][j]
Requirement
Same rows and same columns
Size
Pass rows/cols or use macros
Loops
Outer = rows, inner = columns
Not multiply
Addition is entrywise only
Classic check
{{1,2},{3,4}} + {{5,6},{7,8}} = {{6,8},{10,12}}
Try it
Live Preview
Pick a sample pair to see matrices A, B, and A + B.
Tap a chip, then Add matrices (or tap a chip alone — it runs automatically).
Result
A =
1 2
3 4
B =
5 6
7 8
A + B =
6 8
10 12
Trace
Worked Walkthrough — 2×2
Visit every index once and add the matching cells.