Saturday, July 28, 2012

Program to Add Two Matrices


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int m1[6][6],m2[6][6],res[6][6],r1,c1,r2,c2,i,j;
cout<<"Enter the no. of rows and columns of first matrix\n";
cin>>r1>>c1;
cout<<"Enter the elements of first matrix\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>m1[i][j];
}
}
cout<<"Enter the no. of rows and columns of second matrix\n";
cin>>r2>>c2;
cout<<"Enter the elements of second matrix\n";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cin>>m2[i][j];
}
}
if((r1==r2)&&(c1==c2))
{
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
res[i][j]=m1[i][j]+m2[i][j];
}
}
clrscr();
for(i=0;i<r1;i++)
{
cout<<"\n";
for(j=0;j<c1;j++)
{
cout<<m1[i][j]<<"\t";
}
}
clrscr();
for(i=0;i<r2;i++)
{
cout<<"\n";
for(j=0;j<c2;j++)
{
cout<<m2[i][j]<<"\t";
}
}
clrscr();
cout<<"\nThe sum of two matrix is:\n";
for(i=0;i<r1;i++)
{
cout<<"\n";
for(j=0;j<c1;j++)
{
cout<<res[i][j]<<"\t";
}
}
}
getch();
}



No comments:

Post a Comment