//Write a C++ program to find the sum of two compatible matrices:
#include<iostream.h>
#include<conio.h>
void main( )
{
int a[5][5],b[5][5],s[5][5],i,j,r1,c1,r2,c2;
clrscr();
cout<<"Enter the order of first matrix: ";
cin>>r1>>c1;
cout<<"Enter the order of second matrix: ";
cin>>r2>>c2;
if((r1 == r2) && (c1==c2))
{
cout << "Enter elements of 1st matrix: " << endl;
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
cin >> a[i][j];
cout << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r2; i++)
for(j = 0; j < c2; j++)
cin >> b[i][j];
for(i = 0; i < r1; i++)
for(j = 0; j < c1; j++)
s[i][j] = a[i][j] + b[i][j];
cout << "The resultant matrix is: " << endl;
for(i = 0; i < r1; i++)
{
for(j = 0; j < c1; j++)
cout << s[i][j];
cout << endl;
}
}
else
cout<<"Matrices are not compatible..."<<endl;
getch( );
}

No comments: