0% found this document useful (0 votes)
546 views

Algorithm // Addition of Matrices: Pseudocode

The document contains pseudocode for algorithms to perform addition, subtraction, and multiplication of matrices. The addition algorithm sums the rows and columns of a matrix and prints the results. The subtraction algorithm initializes two matrices, defines a difference matrix, subtracts corresponding elements, and prints the difference matrix. The multiplication algorithm checks if the matrices can be multiplied, initializes a product matrix, calculates each element as the sum of products of corresponding elements, and prints the product matrix.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
546 views

Algorithm // Addition of Matrices: Pseudocode

The document contains pseudocode for algorithms to perform addition, subtraction, and multiplication of matrices. The addition algorithm sums the rows and columns of a matrix and prints the results. The subtraction algorithm initializes two matrices, defines a difference matrix, subtracts corresponding elements, and prints the difference matrix. The multiplication algorithm checks if the matrices can be multiplied, initializes a product matrix, calculates each element as the sum of products of corresponding elements, and prints the product matrix.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Pseudocode

Algorithm // Addition of Matrices


o STEP 1: START
o STEP 2: DEFINE rows, cols, sumRow, sumCol
o STEP 3: INITIALIZE matrix a[][] ={{1, 2, 3},{4, 5, 6}, {7, 8, 9}}
o STEP 4: rows = a.length
o STEP 5: cols = a[0].length
o STEP 6: REPEAT STEP 7 to STEP 10 UNTIL i<rows
// for(i=0; i<rows; i++)
o STEP 7: SET sumRow =0
o STEP 8: REPEAT STEP 9 UNTIL j<cols
o STEP 9: sumRow = sumRow + a[i][j]
o STEP 10: PRINT i+1, sumRow
o STEP 11: REPEAT STEP 12 to STEP 15 UNTIL i<cols
//for(i=0; i<cols; i++)
o STEP 12: SET sumCol =0
o STEP 13: REPEAT STEP 14 UNTIL j<rows
//for(j=0; j<rows; j++)
o STEP 14: sumCol =sumCol + a[j][i]
o STEP 15: PRINT i+1, sumCol
o STEP 16: END

Algorithm // Subtraction of Matrices


o STEP 1: START
o STEP 2: DEFINE rows, cols
o STEP 3: INITIALIZE first matrix a[][] ={{4,5,6},{3,4,1}, {1,2,3}}
o STEP 4: INITIALIZE second matrix b[][] ={{2,0,3}, {2,3,1}{1,1,1}}
o STEP 5: rows = a.length
o STEP 6: cols = a[0].length
o STEP 7: DEFINE diff[][]
o STEP 8: REPEAT STEP 9 to STEP 10 UNTIL i<rows
//for(i=0;i<rows; i++)
o STEP 9: REPEAT STEP 10 UNTIL j<cols
//for(j=0;j<cols; j++)
o STEP 10: diff[i][j] =a[i][j] - b[i][j]
o STEP 11: PRINT "Subtraction of two matrices:"
o STEP 12: REPEAT STEP 13 to STEP 14 UNTIL i<rows
//for(i=0;i<rows; i++)
o STEP 13: REPEAT STEP 14 UNTIL j<cols
//for(j=0; j<cols; j++)
o STEP 13: PRINT diff[i][j]
o STEP 14: PRINT new line
o STEP 15: END

Algorithm // Multiplication
o STEP 1: START
o STEP 2: DEFINE row1, col1, row2, col2
o STEP 3: INITIALIZE matrix a[][] ={{1,3,2},{3,1,1}, {1,2,2}}
o STEP 4: INITIALIZE matrix b[][] ={{2,1,1},{1,0,1}, {1,3,1}}
o STEP 5: row1 = a.length
o STEP 6: col1 = a[0].length
o STEP 7: row2 =b.length
o STEP 8: row2 = b[0].length
o STEP 9: if(col1!=row2)
PRINT "Matrices cannot be multiplied"
else
Go to step 10;
o STEP 10: prod[][] = [row1][col2]
o STEP 11: REPEAT STEP 12 to STEP 14 UNTIL i<row1
//for(i=0; i<row1; i++)
o STEP 12: REPEAT STEP 13 to STEP 14 UNTIL j<col2 // for(j=0; j<col2; j++)
If(j>i) then PRINT 0 else PRINT a[i][j]
o STEP 13: REPEAT STEP 14 UNTIL k<row2 // for(k=0; k<row2; k++)
o STEP 14: prod[i][j] = prod[i][j] + a[i][k]*b[k][j]
o STEP 15: REPEAT STEP 16 to STEP 18 UNTIL i<row1
o STEP 16: REPEAT STEP 17 UNTIL j<col2
o STEP 17: PRINT prod[i][j]
o STEP 18: PRINT new line
o STEP 19: END
Flowchart

You might also like