Matrix multiplication is one of the most basic programs I learned when i was in my 12th standard. Its really simple, simply multiply the rows
of the 1st matrix with the columns of the 2nd matrix and add them up. This will generate the resultant matrix.
The following code demonstrates how it is done. Read it, its quite simple. Still if you encounter any problems or have any other query, please
leave a comment or email me. Any suggestions are also welcome
of the 1st matrix with the columns of the 2nd matrix and add them up. This will generate the resultant matrix.
The following code demonstrates how it is done. Read it, its quite simple. Still if you encounter any problems or have any other query, please
leave a comment or email me. Any suggestions are also welcome
// Program to multiply two matrices
#include <iostream>
int main()
{
int arr1[20][20],arr2[10][10];
int result[20][20];
int m,n;
int i,j;
// prompts the user to enter the rows and columns of the matrix
std::cout<<" Enter the number of rows (m) : ";
std::cin>>m;
std::cout<<" Enter the number of columns (n) : ";
std::cin>>n;
std::cout<<" \n Enter the elements of the 1st matrix \n ";
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
std::cout<<" Element "<<i<<" : "<<j<<" --> ";
std::cin>>arr1[i][j];
}
}
std::cout<<"\n Enter the elements of the 2nd matrix : \n ";
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
std::cout<<" Element "<<i<<" : "<<j<<" --> ";
std::cin>>arr2[i][j];
}
}
//------------ display matrics -----------------------
std::cout<<"\n 1st matrix \n ";
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
std::cout<<" "<<arr1[i][j];
}
std::cout<<"\n";
}
std::cout<<"\n 2nd matrix \n ";
for (i=0; i<m; i++)
{
for (j=0; j<n;j++)
{
std::cout<<" "<<arr2[i][j];
}
std::cout<<"\n";
}
std::cout<<" \n Resultatn matrix \n";
int k=0;
// multiplies the two matrices together
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
result[i][j] = 0;
for(k=0;k< m;k++)
{
result[i][j] = result[i][j] + arr1[i][k] * arr2[k][j];
}
} // end of j sub loop
} // end of i main loop
// displays the resultant matrix
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
std::cout<<result[i][j]<<" ";
}
std::cout<<"\n \n";
}
return 0;
} // end of main
------ OUTPUT ------
Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
do you know how to bubble sort 3 columns string?
ReplyDeleteplease feedback to takeru.darkkabuto@yahoo.com
can matrix multi can take place as
ReplyDeletemulti[i][j]=set1[i][j]*set2[i]j];
plz reply at pranitkadam@gmail.com
ReplyDeletemulti[i][j]=set1[i][j]*set2[i]j];
ReplyDeletethis alone wouldn't solve your purpose of matrix multiplication
you have to store the calculated values
initialiase a matrix res[i][j]=0
then store the multiplied values in a matrix form. display this matrix as your result.Here is the code
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
res[i][j]=0;
for (k=0;k<m;k++)
{
res[i][j] =res[i][j] + set1[i][k] * set2[k][j];
}
}
}
its not generalized since this won't work if (m != n)
ReplyDeleteThanks for this :) I could do a lot of programs but wasn't able to successfully get matrix multiplication done...
ReplyDeletehow could we multiply two matrices if their order is not same?? but their multiplication is possible i.e. no. of columns of first matrix=no. of rows of second matrix?? plz reply soon
ReplyDeletenot getting resultant matrix....i removes std:: before cout coz it showd error in turbo c++
ReplyDeleteWith subtle changes to make it work for generic multiplication.
ReplyDelete#include
using namespace std;
int main()
{
int arr1[20][20],arr2[10][10];
int result[20][20];
int m,n,o,p;
int i,j;
// prompts the user to enter the rows and columns of the matrix
std::cout<<" Enter the number of rows for the first matrix(m) : ";
std::cin>>m;
std::cout<<" Enter the number of columns for the first matrix(n) : ";
std::cin>>n;
std::cout<<" Enter the number of rows for the second matrix(o) : ";
std::cin>>o;
std::cout<<" Enter the number of columns for the second matrix(p) : ";
std::cin>>p;
if(n != o)
{
printf("cannot be multiplied");
return 0;
}
std::cout<<" \n Enter the elements of the 1st matrix \n ";
for (i=0; i ";
std::cin>>arr1[i][j];
}
}
std::cout<<"\n Enter the elements of the 2nd matrix : \n ";
for (i=0; i ";
std::cin>>arr2[i][j];
}
}
//------------ display matrics -----------------------
std::cout<<"\n 1st matrix \n ";
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
std::cout<<" "<<arr1[i][j];
}
std::cout<<"\n";
}
std::cout<<"\n 2nd matrix \n ";
for (i=0; i<o; i++)
{
for (j=0; j<p;j++)
{
std::cout<<" "<<arr2[i][j];
}
std::cout<<"\n";
}
std::cout<<" \n Resultatn matrix \n";
int k=0;
// multiplies the two matrices together
for(i=0;i< m;i++)
{
for(j=0;j< p;j++)
{
result[i][j] = 0;
for(k=0;k< m;k++)
{
result[i][j] = result[i][j] + arr1[i][k] * arr2[k][j];
}
} // end of j sub loop
} // end of i main loop
// displays the resultant matrix
for (i=0; i<m; i++)
{
for (j=0; j<p; j++)
{
std::cout<<result[i][j]<<" ";
}
std::cout<<"\n \n";
}
return 0;
} // end of main
you have to check weather the matrix can be multiplied or not you haven't done so your program is partially wrong!
ReplyDelete(Don't misunderstand me i'm telling you this for your benefit)
I think your program is wrong for multiplying 2 matrix in which rows and columns are as follows: M1(mXn) And M2(nXo)
ReplyDeleteGuys take a chill pill and have look at the code as a reference. He has done a good job.
ReplyDeleteThanks Mate !!
guys plz help me to make this program
ReplyDeletetake one matrix of 3 by 3 and produce null matrix if it don't give null matrix it ask for input again and again
its too tough yaa
ReplyDeletei want c++ code to accept 10 values from user using array and multiply 10 to those values and display the answer
ReplyDeleteThanks ... you just solved my problems. I've been working on this all day :X Thanks a lot :)
ReplyDeleteAfter searching much finally i have found my desired one.... Awesome Code....
ReplyDelete