Matrix Multiplication
Goal:- Multiplication of two Given Matrix.
Method-Used:- Simple Mathematical Logic.
Description:- Simply Ask user to enter two matrix and store them in two different
arrays. Then take a another array and initialize it to zero.
arrays. Then take a another array and initialize it to zero.
If first matrix a is of order (m*n) and second matrix b is of order (n*p)..then resultant matrix will be of order (m*p).
Program:-
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int a[10][10],b[10][10],c[10][10]={};
int m,n,k,i,j,q;
cout<<"\n\tEnter the No. of Rows & Cols in 1st Matrix\n";
cout<<"\t";
cin>>m;
cout<<"\t";
cin>>n;
cout<<"\n\tEnter the elements of 1st Matrix\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"\t";
cin>>a[i][j];
}
}
cout<<"\n\tEnter the No.of Cols in 2nd Matrix(Bcoz Row is Equal to 1st marix's Cols)";
cout<<"\t";
cin>>q;
cout<<"\n\tEnter the Elements of 2nd Matrix\n";
for(i=0;i<n;i++)
{
for(j=0;j<q;j++)
{
cout<<"\t";
cin>>b[i][j];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<q;k++)
{
c[i][k]+=a[i][k]*b[k][j];
}
}
}
cout<<"\t******************************************************\n";
cout<<"\n\t First Matrix:-("<<m<<"*"<<n<<")\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"\t"<<a[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"\tSecond Matrix---("<<n<<"*"<<q<<")\n";
for(i=0;i<n;i++)
{
for(j=0;j<q;j++)
{
cout<<"\t"<<b[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"\tResult OF Multiplication:-("<<m<<"*"<<q<<")\n";
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
cout<<"\t"<<c[i][j]<<"\t";
}
cout<<"\n";
}
getch();
}
0 comments:
Post a Comment