Multiplication of Matrices - concept, program code
In my previous posts, I had briefed about two basic functions that can be performed on a 2 Dimension (2D array, commonly known as matrix) - Addition of two matrices and subtracting one matrix from the other.
In this post I will be discussing the concept of Multiplication of 2 matrices and show you an implementation of the same using a C++ program code.
In this post I will be discussing the concept of Multiplication of 2 matrices and show you an implementation of the same using a C++ program code.
Concept / Algorithm :
Before proceeding we must keep in mind the basic rule of matrix multiplication which is explained as follows:
Consider a matrix A of dimensions m x n, which is to be multiplied with matrix B of size p x q. Multiplication of matrix A with B is possible if and only if m = q. The dimension of the resultant matrix thus formed will be n x p. (The two matrices can be a square matrix each)
If the condition m = q is satisfied, multiply each consecutive column of the matrix A with each rows of the matrix B one after the other. The result gets stored in the corresponding intersecting location. Look on for the example below to get a clear vision on the concept.
Consider a matrix A of dimensions m x n, which is to be multiplied with matrix B of size p x q. Multiplication of matrix A with B is possible if and only if m = q. The dimension of the resultant matrix thus formed will be n x p. (The two matrices can be a square matrix each)
If the condition m = q is satisfied, multiply each consecutive column of the matrix A with each rows of the matrix B one after the other. The result gets stored in the corresponding intersecting location. Look on for the example below to get a clear vision on the concept.
Example :
The example below illustrated the multiplication of two matrices. Matrix A has dimension of 2x3 and Matrix B is of 3x2.
(1, 2, 3) • (7, 9, 11) = 1×7 + 2×9 + 3×11 = 58
(1, 2, 3) • (8, 10, 12) = 1×8 + 2×10 + 3×12 = 64
(4, 5, 6) • (7, 9, 11) = 4×7 + 5×9 + 6×11 = 139
(4, 5, 6) • (8, 10, 12) = 4×8 + 5×10 + 6×12 = 154
Running Program Code :
#include<iostream.h>
#include<process.h>
#include<iomanip.h>
#include<conio.h>
const int max=10;
void main()
{
clrscr();
char ch;
int A[max][max],B[max][max],C[max][max],n,m,p,q,i,j,k;
cout<<"\n\n\t\tTHIS WILL MULTIPLY TWO MATRICES\t\t\n"<<endl;
cout<<"\n\nFor multipling two matrices, both should be square matrix
\n and of equal dimension\n"<<endl;
do
{
cout<<"\nEnter dimensions of first matrix : \n";
cout<<"\n\nNumber of ROWS : ";
cin>>m;
cout<<"\n\nNumber of COLUMNS :";
cin>>n;
if(m>max || n>max)
cout<<"\n\nEnter dimension less than "<<max;
}
while(m>max || n>max);
cout<<endl<<endl;
do
{
cout<<"\nEnter dimensions of second matrix : \n";
cout<<"\n\nNumber of ROWS : ";
cin>>p;
cout<<"\n\nNumber of COLUMNS :";
cin>>q;
if(p>max || q>max)
cout<<"\n\nEnter dimension less than "<<max;
}
while(p>max || q>max);
if(n==p)
{
cout<<endl<<endl;
cout<<"\n\nEnter the FIRST matix (row wise) : "<<endl;
for(i=0; i<m; ++i)
{
for(j=0; j<n; ++j)
{
cout<<"\nElement at position ["<<i<<"]["<<j<<"] : ";
cin>>A[i][j];
}
}
cout<<"\n\nThe FIRST matrix is : \n\n";
for(i=0; i<m; ++i)
{
cout<<"\n\t\t";
for(j=0; j<n; ++j)
{
cout<<setw(5)<<A[i][j];
}
cout<<"\n\n";
}
cout<<endl;
cout<<"\n\nEnter the SECOND matix (row wise) : "<<endl;
for(i=0; i<p; ++i)
{
for(j=0; j<q; ++j)
{
cout<<"\nElement at position ["<<i<<"]["<<j<<"] : ";
cin>>B[i][j];
}
}
cout<<"\n\nThe SECOND matrix is : \n\n";
for(i=0; i<p; ++i)
{
cout<<"\n\t\t";
for(j=0; j<q; ++j)
{
cout<<setw(5)<<B[i][j];
}
cout<<"\n\n";
}
cout<<endl;
for(i=0; i<m; ++i)
{
cout<<"\n";
for(j=0; j<q; ++j)
{
C[i][j]=0;
for(int k=0; k<n; ++k)
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}
}
cout<<"\n\nThe product is : ";
for(i=0; i<m; ++i)
{
cout<<"\n\t\t";
for(j=0; j<q; ++j)
{
cout<<setw(7)<<C[i][j];
}
cout<<"\n\n";
}
}
else
cout<<"\n\nSorry!!!! Product not possible";
getch();
}
Thanks for reading the post…If you have any query regarding the concept or the program code, mention it in the comment below or ask us on Facebook or Google Plus.





Comments
Post a Comment