C++ Program to add two matrix
Program
#include <iostream>
using namespace std;
int main()
{
int a[10][10], b[10][10], c[10][10];
int m, n, i, j;
cout << "Enter the number of rows and columns of a matrix" << endl;
cin >> m >> n;
cout << "Enter " << (m * n) << " elements for matrix 1:" << endl;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin >> a[i][j];
cout << "Enter " << (m * n) << " elements for matrix 2:" << endl;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin >> b[i][j];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
c[i][j] = a[i][j] + b[i][j];
cout << "Sum of matrix is:" << endl;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cout << c[i][j] << "\t";
}
cout << endl;
}
}
Output
Enter the number of rows and columns of a matrix
3
3
Enter 9 elements for matrix 1:
56
32
12
98
47
27
13
48
57
Enter 9 elements for matrix 2:
16
90
78
87
45
20
49
91
40
Sum of matrix is:
72 122 90
185 92 47
62 139 97