Matrix Multiplication in C++

Matrix multiplication in C++ is the process of multiplying two matrices to obtain a new matrix. It involves multiplying corresponding elements and summing them up to form the resulting matrix.

Example:
Suppose we have two matrices:

Matrix A: \( \begin{bmatrix}
1 & 2\\
3 & 4
\end{bmatrix}\) and Matrix B: \( \begin{bmatrix}
4 & 3\\
2 & 1
\end{bmatrix}\)

To perform matrix multiplication, we multiply corresponding elements and sum them up:

Resultant Matrix C:

\( \begin{bmatrix}
1*4 + 2*2 & 1*3 + 2*1\\
3*4 + 4*2 & 3*3 + 4*1
\end{bmatrix}\) = \( \begin{bmatrix}
8 & 5\\
20 & 13
\end{bmatrix}\)

The resultant matrix C is the product of matrices A and B.

Problem Description

Write a C++ Program to Perform Matrix Multiplication.

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

C++ Program/Source code

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

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

Time Complexity: O(r1 * c1 * r2)
The time complexity for matrix multiplication is O(r1 * c1 * r2), where r1, c1, and r2 are the number of rows and columns of matrices A and B, respectively. The three nested loops contribute to this time complexity.

Space Complexity: O(r1 * c2)
The space complexity of the code is O(r1 * c2) because it requires a new matrix C to store the product of matrices A and B. The size of matrix C depends on the number of rows of matrix A (r1) and the number of columns of matrix B (c2).

Runtime Test Cases

Testcase 1: We enter the number of rows and columns for both matrices A and B. We provide the elements for both matrices and check the product.

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

Testcase 2: In this case, the columns of the first matrix are not equal to the rows of the second matrix.

advertisement
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!

Testcase 3: In this case, we input the number of rows and columns for both matrices A and B. Provide elements for Matrix A and Matrix B. Program performs multiplication and outputs the product matrix.

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

To practice programs on every topic in C++, please visit “Programming Examples in C++”, “Data Structures in C++” and “Algorithms in C++”.

advertisement

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.