You are on page 1of 3

/* WRITE A PROGRAM TO FIND ADDITION AND MULTIPLICATION OF 2

MATRICES USING 2-D ARRAYS */


#include <iostream.h>
#include <conio.h>
void main()
{
int a[10][10],m,n,p,q,i,j,s,k,b[10][10],c[10][10];
clrscr();
cout<<"enter the row and cloumn of matrix A=";
cin>>m>>n;
cout<<"enter the row and column of matrix B=";
cin>>p>>q;
if((m!=q)&&(n!=p))
{
cout<<"sorry";
getch();
// exit(0);
}
cout<<"enter the elements of matrix A=";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"enter the elements of matrix B=";
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
cin>>b[i][j];
}
}
cout<<"the matrix A is="<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n";
}
cout<<"the matrix B is="<<endl;
for(i=0;i<p;i++)

{
for(j=0;j<n;j++)
{
cout<<"\t"<<b[i][j];
}
cout<<"\n";
}
cout<<"\n1.ADDITION";
cout<<"\n2.MULTIPLICATION";
cout<<"\n enter the choice=";
cin>>s;
switch(s)
{
case 1:
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<"the C matrix is="<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<"\n";
}
break;
case 2:
cout<<"\n";
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}

cout<<"the C matrix is=\n";


for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<"\n";
}
break;
}
getch();
}
/********* OUTPUT **********
enter the row and cloumn of matrix A=2 2
enter the row and column of matrix B=2 2
enter the elements of matrix A=2 3
34
enter the elements of matrix B=3 2
24
the matrix A is=
2
3
3
4
the matrix B is=
3
2
2
4
1.ADDITION
2.MULTIPLICATION
enter the choice=2
the C matrix is=
12 16
17 22

*/

You might also like