C++ program to multiply two matrix. Here I have given four programs two multiply two matrix like using pointer, functions, using operator overloading .
C++ program to multiply two matrices
#include<iostream>
using namespace std;
int main()
{
int c[10][10],a[10][10],b[10][10],row1,col1,row2,col2,sum=0;
cout<<"Enter row and column of first matrix (max 10*10): ";
cin>>row1>>col1;
cout<<"Enter row and column of second matrix (max 10*10): ";
cin>>row2>>col2;
if(col1!=row2)
{
cout<<"Matrix cannot be multiplied ";
return 1;
}
cout<<"Enter matrix a1 : "<<endl;
for(int i=0;i<row1;i++)
for(int j=0;j<col1;j++)
cin>>a[i][j];
cout<<"Enter matrix a2 : "<<endl;
for(int i=0;i<row2;i++)
for(int j=0;j<col2;j++)
cin>>b[i][j];
for(int i=0;i<row1;i++)
{
for(int j=0;j<col2;j++)
{
for(int k=0;k<col1;k++)
sum+=a[i][k]*b[k][j];
c[i][j]=sum;
sum=0;
}
}
cout<<"Multiplication of two matrix : "<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col2;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
return 0;
}
using namespace std;
int main()
{
int c[10][10],a[10][10],b[10][10],row1,col1,row2,col2,sum=0;
cout<<"Enter row and column of first matrix (max 10*10): ";
cin>>row1>>col1;
cout<<"Enter row and column of second matrix (max 10*10): ";
cin>>row2>>col2;
if(col1!=row2)
{
cout<<"Matrix cannot be multiplied ";
return 1;
}
cout<<"Enter matrix a1 : "<<endl;
for(int i=0;i<row1;i++)
for(int j=0;j<col1;j++)
cin>>a[i][j];
cout<<"Enter matrix a2 : "<<endl;
for(int i=0;i<row2;i++)
for(int j=0;j<col2;j++)
cin>>b[i][j];
for(int i=0;i<row1;i++)
{
for(int j=0;j<col2;j++)
{
for(int k=0;k<col1;k++)
sum+=a[i][k]*b[k][j];
c[i][j]=sum;
sum=0;
}
}
cout<<"Multiplication of two matrix : "<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col2;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
return 0;
}
Method 2 : C++ program to multiply two matrices using pointer
#include<iostream>
using namespace std;
int main()
{
int *p,*q,ord,sum=0;
cout<<"Enter order of matrix : ";
cin>>ord;
p=new int[ord*ord];
q=new int[ord*ord];
cout<<"Enter first matrix : "<<endl;
for(int i=0;i<ord;i++)
for(int j=0;j<ord;j++)
cin>>p[i*ord+j];
cout<<"Enter Second matrix : "<<endl;
for(int i=0;i<ord;i++)
for(int j=0;j<ord;j++)
cin>>q[i*ord+j];
cout<<"Multiplication of two matrix "<<endl;
for(int i=0;i<ord;i++)
{
for(int j=0;j<ord;j++)
{
for(int k=0;k<ord;k++)
sum+=p[i*ord+k]*q[k*ord+j];
cout<<sum<<" ";
sum=0;
}
cout<<endl;
}
return 0;
}
using namespace std;
int main()
{
int *p,*q,ord,sum=0;
cout<<"Enter order of matrix : ";
cin>>ord;
p=new int[ord*ord];
q=new int[ord*ord];
cout<<"Enter first matrix : "<<endl;
for(int i=0;i<ord;i++)
for(int j=0;j<ord;j++)
cin>>p[i*ord+j];
cout<<"Enter Second matrix : "<<endl;
for(int i=0;i<ord;i++)
for(int j=0;j<ord;j++)
cin>>q[i*ord+j];
cout<<"Multiplication of two matrix "<<endl;
for(int i=0;i<ord;i++)
{
for(int j=0;j<ord;j++)
{
for(int k=0;k<ord;k++)
sum+=p[i*ord+k]*q[k*ord+j];
cout<<sum<<" ";
sum=0;
}
cout<<endl;
}
return 0;
}
Method 3 : C++ program to multiply two matrices using functions
#include<iostream>
using namespace std;
void mul(int [10][10],int [10][10],int (&)[10][10],int,int,int);
int main()
{
int a[10][10],b[10][10],row1,col1,row2,col2,c[10][10];
cout<<"Enter row and column of first matrix (max 10*10): ";
cin>>row1>>col1;
cout<<"Enter row and column of second matrix (max 10*10): ";
cin>>row2>>col2;
if(col1!=row2)
{
cout<<"Matrix cannot be multiplied ";
return 1;
}
cout<<"Enter elements of first matrix : "<<endl;
for(int i=0;i<row1;i++)
for(int j=0;j<col1;j++)
cin>>a[i][j];
cout<<"Enter elements of second matrix : "<<endl;
for(int i=0;i<row2;i++)
for(int j=0;j<col2;j++)
cin>>b[i][j];
mul(a,b,c,row1,col2,col1);
cout<<"Multiplication of two matrices : "<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col2;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
return 0;
}
void mul(int a[10][10],int b[10][10],int (&c)[10][10],int row,int col2,int col1)
{
int sum=0;
for(int i=0;i<row;i++)
{
for(int j=0;j<col2;j++)
{
for(int k=0;k<col1;k++)
sum+=a[i][k]*b[k][j];
c[i][j]=sum;
sum=0;
}
}
}
using namespace std;
void mul(int [10][10],int [10][10],int (&)[10][10],int,int,int);
int main()
{
int a[10][10],b[10][10],row1,col1,row2,col2,c[10][10];
cout<<"Enter row and column of first matrix (max 10*10): ";
cin>>row1>>col1;
cout<<"Enter row and column of second matrix (max 10*10): ";
cin>>row2>>col2;
if(col1!=row2)
{
cout<<"Matrix cannot be multiplied ";
return 1;
}
cout<<"Enter elements of first matrix : "<<endl;
for(int i=0;i<row1;i++)
for(int j=0;j<col1;j++)
cin>>a[i][j];
cout<<"Enter elements of second matrix : "<<endl;
for(int i=0;i<row2;i++)
for(int j=0;j<col2;j++)
cin>>b[i][j];
mul(a,b,c,row1,col2,col1);
cout<<"Multiplication of two matrices : "<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col2;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
return 0;
}
void mul(int a[10][10],int b[10][10],int (&c)[10][10],int row,int col2,int col1)
{
int sum=0;
for(int i=0;i<row;i++)
{
for(int j=0;j<col2;j++)
{
for(int k=0;k<col1;k++)
sum+=a[i][k]*b[k][j];
c[i][j]=sum;
sum=0;
}
}
}
C++ to multiply two matrices using operator overloading
#include<iostream>
using namespace std;
struct mul
{
int a[3][3];
};
mul operator*(mul mat1,mul mat2)
{
mul mat3;
int sum=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for(int k=0;k<3;k++)
sum+=mat1.a[i][k]*mat2.a[k][j];
mat3.a[i][j]=sum;
sum=0;
}
}
return mat3;
}
int main()
{
mul mat1,mat2,mat3;
cout<<"Enter elements of first matrix : "<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
cin>>mat1.a[i][j];
cout<<"Enter elements of second matrix : "<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
cin>>mat2.a[i][j];
mat3=mat1*mat2;
cout<<"Multiplication of two matrices : "<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout<<mat3.a[i][j]<<" ";
cout<<endl;
}
return 0;
}
using namespace std;
struct mul
{
int a[3][3];
};
mul operator*(mul mat1,mul mat2)
{
mul mat3;
int sum=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for(int k=0;k<3;k++)
sum+=mat1.a[i][k]*mat2.a[k][j];
mat3.a[i][j]=sum;
sum=0;
}
}
return mat3;
}
int main()
{
mul mat1,mat2,mat3;
cout<<"Enter elements of first matrix : "<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
cin>>mat1.a[i][j];
cout<<"Enter elements of second matrix : "<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
cin>>mat2.a[i][j];
mat3=mat1*mat2;
cout<<"Multiplication of two matrices : "<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout<<mat3.a[i][j]<<" ";
cout<<endl;
}
return 0;
}
No comments:
Post a Comment