On this page we will write a basic C program to add two matrices for understanding basic structure of multidimensional array in C programming. In programming , we can add two or more matrices easily by using proper syntax and algorithm.
Algorithm For C Program to Add Two Matrices :
To add two matrices the number of row and number of columns in both the matrices must be same the only addition can take place.
Step 1: Start
Step 2: Declare matrix mat1[row][col]; and matrix mat2[row][col]; and matrix sum[row][col]; row= no. of rows, col= no. of columns
Step 3: Read row, col, mat1[][] and mat2[][]
Step 4: Declare variable i=0, j=0
Step 5: Repeat until i < row
5.1: Repeat until j < col
sum[i][j]=mat1[i][j] + mat2[i][j]
Set j=j+1
5.2: Set i=i+1
Step 6: sum is the required matrix after addition
Step 7: Stop
In the above algorithm,
using scanf( ) will take the input from the user rows,columns,elements of both the matrix.
we will the add the element of matrix 1 and matrix 2 in third matrix let say sum matrix.
printf( ) will print the output on the screen
we have to use the for loop to input the element of matrix ,sum the elements of matrices and print the final result.
Login/Signup to comment