In this program, the user is asked to enter the elements of the two matrices a and b. Then the elements of these two matrices are added and saved it in third matrix. Finally, the result (third matrix) is printed on the screen.
#include<iostream>
using namespace std;
int main()
{
int a[3][3], b[3][3], c[3][3], i, j;
cout<<"Enter the first matrix\n";
for(i=0;i <3; i++)
{
for(j=0; j<3; j++)
{
cin>> a[i][j];
}
}
cout<<"Enter the second matrix\n";
for(i=0;i <3; i++)
{
for(j=0; j<3; j++)
{
cin>> b[i][j];
}
}
cout<<"Addition of matrices A & B is\n";
for(i=0;i <3; i++)
{
for(j=0; j<3; j++)
{
c[i][j] = a[i][j] + b[i][j];
cout<< c[i][j] << " ";
}
cout<< "\n";
}
}
Enter the first matrix
1 2 4
4 2 1
8 6 3
Enter the second matrix
1 7 2
5 3 4
8 6 4
Addition of matrices A & B is
2 9 6
9 5 5
16 12 7