Here is a C++ program to perform chain multiplication of matrices
SOURCE CODE OUTPUT
#include<iostream>
using namespace std;
void input(int (&inp)[10][10],int ord)
{
for(int i=0;i<ord;i++)
for(int j=0;j<ord;j++)
cin>>inp[i][j];
}
void mult(int (&a)[10][10],int (&b)[10][10],int (&c)[10][10],int ord)
{
int sum=0;
for(int i=0;i<ord;i++)
{
for(int j=0;j<ord;j++)
{
for(int k=0;k<ord;k++)
sum+=a[i][k]*b[k][j];
c[i][j]=sum;
sum=0;
}
}
}
int main()
{
int c[10][10],a[10][10],b[10][10],ord,sum=0,n;
cout<<"Enter order of matrix (max 10*10): ";
cin>>ord;
cout<<"How many matrix to be multiplied : ";
cin>>n;
cout<<"Enter matrix : "<<endl;
input(a,ord);
cout<<"Enter matrix : "<<endl;
input(b,ord);
for(int i=0;i<n-2;i++)
{
mult(a,b,c,ord);
for(int j=0;j<ord;j++)
for(int k=0;k<ord;k++)
b[j][k]=c[j][k];
cout<<"Enter matrix : "<<endl;
input(a,ord);
}
mult(a,b,c,ord);
cout<<"Multiplication of "<<n<<" matrix : "<<endl;
for(int i=0;i<ord;i++)
{
for(int j=0;j<ord;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
return 0;
}
using namespace std;
void input(int (&inp)[10][10],int ord)
{
for(int i=0;i<ord;i++)
for(int j=0;j<ord;j++)
cin>>inp[i][j];
}
void mult(int (&a)[10][10],int (&b)[10][10],int (&c)[10][10],int ord)
{
int sum=0;
for(int i=0;i<ord;i++)
{
for(int j=0;j<ord;j++)
{
for(int k=0;k<ord;k++)
sum+=a[i][k]*b[k][j];
c[i][j]=sum;
sum=0;
}
}
}
int main()
{
int c[10][10],a[10][10],b[10][10],ord,sum=0,n;
cout<<"Enter order of matrix (max 10*10): ";
cin>>ord;
cout<<"How many matrix to be multiplied : ";
cin>>n;
cout<<"Enter matrix : "<<endl;
input(a,ord);
cout<<"Enter matrix : "<<endl;
input(b,ord);
for(int i=0;i<n-2;i++)
{
mult(a,b,c,ord);
for(int j=0;j<ord;j++)
for(int k=0;k<ord;k++)
b[j][k]=c[j][k];
cout<<"Enter matrix : "<<endl;
input(a,ord);
}
mult(a,b,c,ord);
cout<<"Multiplication of "<<n<<" matrix : "<<endl;
for(int i=0;i<ord;i++)
{
for(int j=0;j<ord;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
return 0;
}
Any questions regarding to program please write in comments.
No comments:
Post a Comment