Matrix Addition: flowchart, algorithm and program
This program reads two 2×2 matrices, a and b, and stores their sum in a third matrix called sum. Matrices of the same size are added element by element, so sum[i][j] = a[i][j] + b[i][j].
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Print 'Enter first matrix'.
- Step 3: Set i = 0.
- Step 4: If i < 2, go to step 5; otherwise go to step 10.
- Step 5: Set j = 0.
- Step 6: If j < 2, go to step 7; otherwise go to step 9.
- Step 7: Read a[i][j].
- Step 8: Increase j by 1. Go to step 6.
- Step 9: Increase i by 1. Go to step 4.
- Step 10: Print 'Enter second matrix'.
- Step 11: Set i = 0.
- Step 12: If i < 2, go to step 13; otherwise go to step 18.
- Step 13: Set j = 0.
- Step 14: If j < 2, go to step 15; otherwise go to step 17.
- Step 15: Read b[i][j].
- Step 16: Increase j by 1. Go to step 14.
- Step 17: Increase i by 1. Go to step 12.
- Step 18: Set i = 0.
- Step 19: If i < 2, go to step 20; otherwise go to step 25.
- Step 20: Set j = 0.
- Step 21: If j < 2, go to step 22; otherwise go to step 24.
- Step 22: Set sum[i][j] = a[i][j] + b[i][j].
- Step 23: Increase j by 1. Go to step 21.
- Step 24: Increase i by 1. Go to step 19.
- Step 25: Print 'Sum of matrices'.
- Step 26: Set i = 0.
- Step 27: If i < 2, go to step 28; otherwise go to step 34.
- Step 28: Set j = 0.
- Step 29: If j < 2, go to step 30; otherwise go to step 32.
- Step 30: Print sum[i][j].
- Step 31: Increase j by 1. Go to step 29.
- Step 32: Print newline.
- Step 33: Increase i by 1. Go to step 27.
- Step 34: Stop.
Pseudocode
BEGIN
PRINT 'Enter first matrix'
i ← 0
WHILE i < 2 DO
j ← 0
WHILE j < 2 DO
READ a[i][j]
j++
END WHILE
i++
END WHILE
PRINT 'Enter second matrix'
i ← 0
WHILE i < 2 DO
j ← 0
WHILE j < 2 DO
READ b[i][j]
j++
END WHILE
i++
END WHILE
i ← 0
WHILE i < 2 DO
j ← 0
WHILE j < 2 DO
sum[i][j] ← a[i][j] + b[i][j]
j++
END WHILE
i++
END WHILE
PRINT 'Sum of matrices'
i ← 0
WHILE i < 2 DO
j ← 0
WHILE j < 2 DO
PRINT sum[i][j]
j++
END WHILE
PRINT newline
i++
END WHILE
END
Program in C, Python and Java
Matrix Addition program in C
#include <stdio.h>
int main() {
int a[2][2], b[2][2], sum[2][2];
int i, j;
printf("Enter elements of first matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("Sum of matrices:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}Matrix Addition program in Python
a = [[0, 0], [0, 0]]
b = [[0, 0], [0, 0]]
sum = [[0, 0], [0, 0]]
print("Enter elements of first matrix:")
for i in range(2):
for j in range(2):
a[i][j] = int(input())
print("Enter elements of second matrix:")
for i in range(2):
for j in range(2):
b[i][j] = int(input())
for i in range(2):
for j in range(2):
sum[i][j] = a[i][j] + b[i][j]
print("Sum of matrices:")
for i in range(2):
for j in range(2):
print(sum[i][j], end=" ")
print()Matrix Addition program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] a = new int[2][2];
int[][] b = new int[2][2];
int[][] sum = new int[2][2];
int i, j;
System.out.println("Enter elements of first matrix:");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter elements of second matrix:");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
b[i][j] = sc.nextInt();
}
}
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("Sum of matrices:");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
}
}
Sample input: A = 1 2 3 4, B = 5 6 7 8
Output: Sum of matrices:
6 8
10 12
How it works
- Three 2×2 arrays are declared: a and b hold the input, and sum holds the result. The variables i and j are the row and column counters.
- Two nested loops read the first matrix and then the second. The outer loop i picks the row and the inner loop j picks the column, so scanf fills a[i][j] one cell at a time in row order.
- A third pair of nested loops does the addition: for every position (i, j) it computes sum[i][j] = a[i][j] + b[i][j]. Each cell depends only on the same cell in a and b.
- A last pair of nested loops prints sum. The inner loop prints each value of a row followed by a space, and printf("\n") after the inner loop moves to the next line, so the output looks like a matrix.
- Dry run with A = 1 2 3 4 and B = 5 6 7 8: sum[0][0] = 1+5 = 6, sum[0][1] = 2+6 = 8, sum[1][0] = 3+7 = 10, sum[1][1] = 4+8 = 12. The program prints 6 8 on the first line and 10 12 on the second.
Dry run (trace table)
| Step | Line | Box | i | j | a[0][0] | a[0][1] | a[1][0] | a[1][1] | b[0][0] | b[0][1] | b[1][0] | b[1][1] | Output |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 3 | Start | |||||||||||
| 2 | 7 | Print 'Enter first matrix' | Enter elements of first matrix: | ||||||||||
| 3 | 8 | i = 0 | 0 | ||||||||||
| 4 | 8 | i < 2? | 0 | ||||||||||
| 5 | 9 | j = 0 | 0 | 0 | |||||||||
| 6 | 9 | j < 2? | 0 | 0 | |||||||||
| 7 | 10 | Read a[i][j] | 0 | 0 | 1 | ||||||||
| 8 | 9 | j++ | 0 | 1 | 1 | ||||||||
| 9 | 9 | j < 2? | 0 | 1 | |||||||||
| 10 | 10 | Read a[i][j] | 0 | 1 | 2 | ||||||||
| 11 | 9 | j++ | 0 | 2 | |||||||||
| 12 | 9 | j < 2? | 0 | 2 | |||||||||
| 13 | 8 | i++ | 1 | 2 | |||||||||
| 14 | 8 | i < 2? | 1 | 2 | |||||||||
| 15 | 9 | j = 0 | 1 | 0 | |||||||||
| 16 | 9 | j < 2? | 1 | 0 | |||||||||
| 17 | 10 | Read a[i][j] | 1 | 0 | 3 | ||||||||
| 18 | 9 | j++ | 1 | 1 | |||||||||
| 19 | 9 | j < 2? | 1 | 1 | |||||||||
| 20 | 10 | Read a[i][j] | 1 | 1 | 4 |
Time and space complexity
Time: O(n²) · Space: O(n²). Each of the n×n elements (here 2×2) is visited a constant number of times, and the sum matrix stores n×n values.
Common mistakes
- Putting printf("\n") inside the inner loop (or forgetting it), so the output prints as one long line or one number per line instead of rows.
- Forgetting the & in scanf("%d", &a[i][j]), which makes scanf write to the wrong address and usually crashes the program.
- Using the wrong loop limits or swapping i and j, which reads or prints the elements in the wrong order or goes outside the array.
Viva questions
Q1. Why do the loops run from 0 to less than 2 (i < 2) for a 2x2 matrix?
C arrays are zero-indexed, so a 2x2 matrix has indices 0 and 1. Using i < 2 covers both and stops before index 2, which would be out of bounds.
Q2. Why do you need nested loops here? What does each loop do?
The outer loop i moves through the rows and the inner loop j moves through the columns of each row. Together they visit every element a[i][j] exactly once.
Q3. Why is there a & in scanf("%d", &a[i][j]) but not in printf?
scanf must be given the address of the element so it can store the typed value there. printf only needs the value to print, so no address is needed.
Q4. Why is printf("\n") placed after the inner loop and not inside it in the printing section?
The newline should be printed once per row, after all the columns of that row are printed. If it were inside the inner loop, every element would appear on its own line and the matrix shape would be lost.
Q5. What is the condition for adding two matrices? What would happen if the sizes were different?
Both matrices must have the same number of rows and columns, because addition is done element by element. With different sizes some elements would have no partner, and the loop would read outside the array bounds.
Q6. What is the time complexity of this program?
For an n x n matrix the addition is O(n^2), since there are n*n elements and each is added once. Here n is 2, so it is just 4 additions, a constant.
Q7. What would you change to make this work for an m x n matrix entered by the user?
Read m and n, declare arrays with size big enough (or use a VLA/malloc), and change the loop limits to i < m and j < n. Both matrices must use the same m and n.
Q8. Could you do the addition without the separate sum array? How?
Yes, you can add b[i][j] into a[i][j] (a[i][j] += b[i][j]) or print a[i][j] + b[i][j] directly. This saves memory but destroys the original matrix a if you stored the result in it.
Frequently asked questions
Why do we need nested loops for matrix addition?
A matrix has rows and columns, so one loop goes over the rows (i) and an inner loop goes over the columns (j). Together they visit every cell (i, j) exactly once.
Can we add two matrices of different sizes?
No. Matrix addition needs both matrices to have the same number of rows and columns, because each element is added to the element in the same position of the other matrix.
How do I change this program for a bigger matrix?
Change the array sizes and the loop limits from 2 to your size, for example 3 for a 3×3 matrix. Using a #define for the size makes the change in only one place.