Copyright © Programs ++
Design by Dzignine
Friday, 20 April 2012

Matrix Multiplication in C++

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
// 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.

17 comments:

  1. do you know how to bubble sort 3 columns string?
    please feedback to takeru.darkkabuto@yahoo.com

    ReplyDelete
  2. can matrix multi can take place as

    multi[i][j]=set1[i][j]*set2[i]j];

    ReplyDelete
  3. plz reply at pranitkadam@gmail.com

    ReplyDelete
  4. multi[i][j]=set1[i][j]*set2[i]j];
    this 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];
    }
    }
    }

    ReplyDelete
  5. its not generalized since this won't work if (m != n)

    ReplyDelete
  6. Thanks for this :) I could do a lot of programs but wasn't able to successfully get matrix multiplication done...

    ReplyDelete
  7. how 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

    ReplyDelete
  8. not getting resultant matrix....i removes std:: before cout coz it showd error in turbo c++

    ReplyDelete
  9. With subtle changes to make it work for generic multiplication.

    #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

    ReplyDelete
  10. you have to check weather the matrix can be multiplied or not you haven't done so your program is partially wrong!

    (Don't misunderstand me i'm telling you this for your benefit)

    ReplyDelete
  11. I think your program is wrong for multiplying 2 matrix in which rows and columns are as follows: M1(mXn) And M2(nXo)

    ReplyDelete
  12. Guys take a chill pill and have look at the code as a reference. He has done a good job.

    Thanks Mate !!

    ReplyDelete
  13. guys plz help me to make this program
    take one matrix of 3 by 3 and produce null matrix if it don't give null matrix it ask for input again and again

    ReplyDelete
  14. its too tough yaa

    ReplyDelete
  15. i want c++ code to accept 10 values from user using array and multiply 10 to those values and display the answer

    ReplyDelete
  16. Thanks ... you just solved my problems. I've been working on this all day :X Thanks a lot :)

    ReplyDelete
  17. After searching much finally i have found my desired one.... Awesome Code....

    ReplyDelete

Comment Here....