Algorithm and Flowchart for Matrix Multiplication

[6138 views]


Matrix is an array of Numbers. It can have any number of rows and any number of columns.

Now to multiply 2 matrices of multi-dimensions we need to take input from the user: input includes number of rows, columns, first matrix elements and second matrix elements. Then we perform multiplication on the matrices entered by the user and store it in some other matrix.

Algorithm for Matrix Multiplication

In matrix multiplication, one row element of first matrix is individually multiplied by all column elements and added. Likewise, for every row element same proceedure is followed and we get the elements.

Flowchart for Matrix multiplication :

Flowchart for Matrix multiplication

Algorithm for Matrix multiplication :

Step 1: Start Step 2: Declare matrix A[m][n] and matrix B[p][q] and matrix C[m][q] Step 3: Read m, n, p, q. Step 4: Now check if the matrix can be multiplied or not, if n is not equal to q matrix can't be multiplied and an error message is generated. Step 5: Read A[][] and B[][] Step 4: Declare variable i=0, k=0 , j=0 and sum=0 Step 5: Repeat Step until i < m 5.1: Repeat Step until j < q 5.1.1: Repeat Step until k < p Set sum= sum + A[i][k] * B[k][j] Set multiply[i][j] = sum; Set sum = 0 and k=k+1 5.1.2: Set j=j+1 5.2: Set i=i+1 Step 6: C is the required matrix. Step 7: Stop

In the above algorithm,

  1. We first define three matrices A, B, C and read their respective row and column numbers in variable m, n, p and q.
  2. We check if the matrix can be multiplied or not, if n is not equal to q matrix can't be multiplied and an error message is generated.
  3. Read matrices A and B.
  4. First, start a loop which goes upto m giving row elements of A
    Secondly, inside it again start a loop which goes upto p giving row elements of B.
    At last, we define a loop which goes upto p giving column element of B
  5. Then, we store their corresponding multiplication by sum= sum + A[i][k] * B[k][j], which gets updated each time till k reaches p, which acts as the mathematical formula of multiplication used for matrix.
  6. sum is assigned into C[i][j] and likewise, C stores the multiplication result of matrix A and B

Recommended:

        

Algorithm Logic Test

Our Quiz prepared by Experts Helps you identify your knowledge in Algorithms. Everyone should atleast attempt this Quiz Once. 



Need any help in Programming? Chat with Us

Comments



Online Compiler
Search
Algorithm Quiz

Only 5% Users were able to score above 75% in this Quiz. Can You Crack this?

Deals Ends in





Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags

    Pseudocode for Matrix Multiplication

    Algorithm for Matrix Multiplication

    Multiply two matrices algorithm