Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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.


This post first appeared on Programs ++, please read the originial post: here

Share the post

Matrix Multiplication in C++

×

Subscribe to Programs ++

Get updates delivered right to your inbox!

Thank you for your subscription

×