• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
August 16, 2024 |3.0K Views

C++ Program on Multiplication of Two Matrices

  Share  2 Likes
Description
Discussion

In this video, we will write a C++ program to multiply two matrices.

Multiplication is a fundamental operation in mathematics and computer science. In programming, multiplication is performed using two numbers or variables called operands. Operand means something that is operated upon. An operand can be either a variable or a constant, and a matrix represents the collection of numbers arranged in an order of rows and columns. It is necessary to enclose the elements of a matrix in parentheses or brackets.

For Example:
Input: mat1[m][n] = { {1, 1}, {2, 2} }
mat2[p][q] = { {1, 1}, {2, 2} }
Output: result[m][p] = { {3, 3}, {6, 6} }

Input
mat1[m][n]= { {1 2  3},{ 3  2  1},{ 1 2  3}}
mat2[p][q]= { {4 5 6},{ 6 5 4},{4 6 5}}

Output:
{28  33  29
28  31  31
28  33  29}


Multiplication of two Matrices:
The number of columns in Matrix-1 must be equal to the number of rows in Matrix-2.

The output of multiplication of Matrix-1 and Matrix-2 results with equal to the number of rows of Matrix-1 and the number of columns of Matrix-2 i.e. rslt[R1][C2]

Algorithm:
Step 1: First declare  three matrices m1[3][3], m2[3][3], m3[3][3] and variables sum, i,j,k.
Step 2: Now Enter the first and second matrices elements.
Step 3: Run for loop from 0 to result. length as i:
Step 4: Nest run another for loop from 0 to result[0].length as j:
Step 5: Nest another loop from 0 to m1[0].length in k:
Step 6: Sum (m1[I][K] * m2[K][J])
Step 7: Store the sum into the resultant matrix as m3[i][j]=sum.
Step 8: End loop
Step 9: Print the resultant matrix.

C++ program to multiply two matrices:
https://www.geeksforgeeks.org/cpp-program-to-multiply-two-matrices/