C++ Program to multiply two matrix.
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int i,j,k,m1,n1,m2,n2,a[20][20],b[20][20],c[20][20];
cout<<"enter rows and columns of matrix 1";
cin>>r1>>c1;
cout<<"\n enter rows and columns of matrix 2";
cin>>r2>>c2;
if(n1==m2)
{
cout<<"Enter first matrix ";
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
cin>>a[i][j];
cout<<"\n Enter second matrix";
for(i=0;i<m2;i++)
for(j=0;j<n2;j++)
cin>>b[i][j];
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{c[i][j]=0;
for(k=0;k<c1;k++)
{c[i][j]+=a[i][k]*b[k][j];} //remember this
}
}
for(i=0;i<m1;i++)
{for(j=0;j<n2;j++)
cout<<c[i][j]<<" ";
cout<<"\n";
}
}
else
cout<<"\n sorry wrong entry ABORTING!!!!";
getch();
}
#include<conio.h>
void main()
{clrscr();
int i,j,k,m1,n1,m2,n2,a[20][20],b[20][20],c[20][20];
cout<<"enter rows and columns of matrix 1";
cin>>r1>>c1;
cout<<"\n enter rows and columns of matrix 2";
cin>>r2>>c2;
if(n1==m2)
{
cout<<"Enter first matrix ";
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
cin>>a[i][j];
cout<<"\n Enter second matrix";
for(i=0;i<m2;i++)
for(j=0;j<n2;j++)
cin>>b[i][j];
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{c[i][j]=0;
for(k=0;k<c1;k++)
{c[i][j]+=a[i][k]*b[k][j];} //remember this
}
}
for(i=0;i<m1;i++)
{for(j=0;j<n2;j++)
cout<<c[i][j]<<" ";
cout<<"\n";
}
}
else
cout<<"\n sorry wrong entry ABORTING!!!!";
getch();
}
Comments
Post a Comment