Tuesday 17 January 2012

sum of two matrices


// program to enter two 2D arrays and display their sum in matrix form
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int mat1[3][3],mat2[3][3],sum[3][3],i,j;
cout<<"Enter elements in first matrix"<<endl;
for(i=0;i<3;i++)        //Row
{
for(j=0;j<3;j++)     //Column
{
cin>>mat1[i][j];
}
}
cout<<"Enter elements in second matrix"<<endl;
for(i=0;i<3;i++)        //Row
{
for(j=0;j<3;j++)     //Column
{
cin>>mat2[i][j];
}
}

// loop to sum two matrices
for(i=0;i<3;i++)        //Row
{
for(j=0;j<3;j++)     //Column
{
      sum[i][j]=mat1[i][j]+mat2[i][j];
}
}
//loop to display sum in matrix form
cout<<"The sum of two matrices is "<<endl;
for(i=0;i<3;i++)        //Row
{
for(j=0;j<3;j++)     //Column
{
cout<<sum[i][j]<<"\t";
}
cout<<endl;
}
getch();
}

1 comment: