C++ Program to Perform Matrix Multiplication

«
»

This is a C++ Program to Perform Matrix Multiplication.

Problem Description

The program multiplies two matrices and prints the result.

Problem Solution

1. The program takes two matrices and multiplies them.
2. If number of columns of matrix A is not equal to number of rows of matrix B, then matrices cannot be added.
3. The program is exited.
4. Else they are multiplied and the result is printed.
5. Exit.

advertisement
C++ Program/Source code

Here is the source code of C++ Program to Multiply Two Matrices. The program output is shown below.

  1. #include<iostream>
  2. using namespace std; 
  3. int main ()
  4. {
  5.     int r1, c1, r2, c2, i, j, k;
  6.     int A[5][5], B[5][5], C[5][5];
  7.     cout << "Enter number of rows and columns of matrix A : ";
  8.     cin >> r1 >> c1;
  9.     cout << "Enter number of rows and columns of matrix B : ";
  10.     cin >> r2 >> c2; 
  11.     if (c1 != r2)
  12.     {
  13.         cout << "Matrices cannot be multiplied!";
  14.         exit(0);
  15.     }	
  16.     cout << "Enter elements of matrix A : ";
  17.     for (i = 0; i < r1; i++)
  18.         for (j = 0; j < c1; j++)
  19.             cin >> A[i][j];
  20.     cout << "Enter elements of matrix B : ";
  21.     for (i = 0; i < r2; i++)
  22.         for (j = 0; j < c2; j++)
  23.             cin >> B[i][j];		
  24.     for (i = 0; i < r1; i++)
  25.     {
  26.         for (j = 0; j < c2; j++)
  27.         {
  28.             C[i][j] = 0;
  29.             for (k = 0; k < r2; k++)
  30.             {
  31.                 C[i][j] += A[i][k] * B[k][j];
  32.             }
  33.         }
  34.     }
  35.     cout << "Product of matrices\n";
  36.     for (i = 0; i < r1; i++)
  37.     {    
  38.         for (j = 0; j < c2; j++)
  39.             cout << C[i][j] << "  ";
  40.         cout << "\n";
  41.     }
  42.     return 0;
  43. }
Program Explanation

1. The user is asked to enter number of rows and columns of matrix A and matrix B.
2. If number of columns of matrix A is not equal to number of rows of matrix B, then matrices cannot be multiplied.
3. The program is exited using the function exit().
4. Else if they are equal, matrices are multiplied using for loops and the product is stored in matrix C.
5. The result is then printed.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter number of rows and columns of matrix A : 2 2
Enter number of rows and columns of matrix B : 2 2
Enter elements of matrix A : 1 2 3 4
Enter elements of matrix B : 4 3 2 1
Product of matrices
8  5
20  13
 
Case 2 :
Enter number of rows and columns of matrix A : 1 3                                                                             
Enter number of rows and columns of matrix B : 2 3                                                                             
Matrices cannot be multiplied!
 
Case 3 :
Enter number of rows and columns of matrix A : 1 1
Enter number of rows and columns of matrix B : 1 3
Enter elements of matrix A : 3
Enter elements of matrix B : 1 2 3
Product of matrices
3  6  9

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.