C++ Program to Add Two Matrices with Output
result[i][j] = matrix1[i][j] + matrix2[i][j]; inside two for loops iterating over rows and columns.Examples
How to Think About It
Algorithm
Code
#include <iostream> using namespace std; int main() { int rows = 2, cols = 2; int matrix1[2][2] = {{1, 2}, {3, 4}}; int matrix2[2][2] = {{5, 6}, {7, 8}}; int result[2][2]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result[i][j] = matrix1[i][j] + matrix2[i][j]; } } cout << "Resultant Matrix:\n"; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { cout << result[i][j] << " "; } cout << "\n"; } return 0; }
Dry Run
Let's trace adding matrices [[1, 2], [3, 4]] and [[5, 6], [7, 8]] through the code.
Initialize matrices and result
matrix1 = [[1, 2], [3, 4]], matrix2 = [[5, 6], [7, 8]], result = [[?, ?], [?, ?]]
Add element at row 0, col 0
result[0][0] = 1 + 5 = 6
Add element at row 0, col 1
result[0][1] = 2 + 6 = 8
Add element at row 1, col 0
result[1][0] = 3 + 7 = 10
Add element at row 1, col 1
result[1][1] = 4 + 8 = 12
Print result matrix
Resultant Matrix: 6 8 10 12
| Position | matrix1[i][j] | matrix2[i][j] | result[i][j] |
|---|---|---|---|
| [0][0] | 1 | 5 | 6 |
| [0][1] | 2 | 6 | 8 |
| [1][0] | 3 | 7 | 10 |
| [1][1] | 4 | 8 | 12 |
Why This Works
Step 1: Matrix elements correspond by position
Each element in the first matrix has a matching element in the second matrix at the same row and column, so we add those pairs.
Step 2: Use nested loops to access all elements
Two loops let us visit every row and column index to perform addition on all elements.
Step 3: Store sums in a new matrix
We keep the sums in a separate matrix so the original matrices stay unchanged and we can print the result.
Alternative Approaches
#include <iostream> #include <vector> using namespace std; int main() { vector<vector<int>> matrix1 = {{1, 2}, {3, 4}}; vector<vector<int>> matrix2 = {{5, 6}, {7, 8}}; vector<vector<int>> result(2, vector<int>(2)); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { result[i][j] = matrix1[i][j] + matrix2[i][j]; } } cout << "Resultant Matrix:\n"; for (auto &row : result) { for (auto &val : row) cout << val << " "; cout << "\n"; } return 0; }
#include <iostream> using namespace std; int main() { int rows, cols; cout << "Enter rows and columns: "; cin >> rows >> cols; int matrix1[10][10], matrix2[10][10], result[10][10]; cout << "Enter first matrix:\n"; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) cin >> matrix1[i][j]; cout << "Enter second matrix:\n"; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) cin >> matrix2[i][j]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) result[i][j] = matrix1[i][j] + matrix2[i][j]; cout << "Resultant Matrix:\n"; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) cout << result[i][j] << " "; cout << "\n"; } return 0; }
Complexity: O(rows * cols) time, O(rows * cols) space
Time Complexity
The program uses two nested loops to visit each element once, so the time grows with the total number of elements.
Space Complexity
A new matrix is created to store the sum, so space grows with the size of the matrices.
Which Approach is Fastest?
Using arrays or vectors both have similar time complexity; vectors add flexibility but may have slight overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Static arrays | O(rows * cols) | O(rows * cols) | Fixed size, simple cases |
| Vectors | O(rows * cols) | O(rows * cols) | Dynamic size, safer memory |
| User input with arrays | O(rows * cols) | O(rows * cols) | Flexible size with input |