We can add, subtract, multiply and divide 2 matrices of multi-dimensions. For any operations, we first take input from the user the number of rows in matrix, columns in matrix, first matrix elements and second matrix elements. Then we perform required operations on the matrices entered by the user and store it in some other matrix.
In matrix addition, one row element of first matrix is individually added to corresponding column elements i.e., for 1st element at Position[0,0], the addition of first Matrix's Position[0,0] will be added with Second Matrix's Position[0,0]. Let's see this in Mathematical equation, if
A and B are the Matrix entered by user and we are storing addition in Matrix C, then
for Position[0,0]: C[0,0]=A[0,0]+B[0,0]
for Position[0,1]: C[0,1]=A[0,1]+B[0,1]
Similarly we perform addition of each element for corresponding Position.
Flowchart for Matrix addition
Pseudocode for Matrix addition
Step 1: Start
Step 2: Declare matrix A[r][c];
and matrix B[r][c];
and matrix C[r][c]; r= no. of rows, c= no. of columns
Step 3: Read r, c, A[][] and B[][]
Step 4: Declare variable i=0, j=0
Step 5: Repeat until i < r
5.1: Repeat until j < c
C[i][j]=A[i][j] + B[i][j]
Set j=j+1
5.2: Set i=i+1
Step 6: C is the required matrix after addition
Step 7: Stop
In the above algorithm,
- We first define three matrices A, B, C and read their respective row and column numbers in variable r and c
- Read matrices A and B.
- First, start a loop for getting row elements of A and B
Secondly, inside it again start a loop for column of A and B
- Then, we store their corresponding addition by C[i][j]=A[i][j] + B[i][j] into C[i][j]
- At the end of loop, the result of addition is stored in Matrix C
Recommended: