The program to add two matrices is discussed here. Two matrices are obtained as input from the user. The addition of two matrices is possible only when both the matrices contain same number of rows and columns.
@@coding::1@@
#include<stdio.h>n#include<stdlib.h>nint i,j;nint** memory(int c,int r){nint **a;na=(int **)malloc(c*sizeof(int*));nfor(i=0;i<c;i++)n*(a+i)=(int *)malloc(r*sizeof(int));nreturn a;n}nint main()n{nint **a,**b,r,c;nscanf(“%d %dâ€,&r,&c);na=memory(c,r);nb=memory(c,r);nfor(i=0;i<r;i++){nfor(j=0;j<c;j++){nscanf(“%dâ€,&a[i][j]);n}n}nfor(i=0;i<r;i++){nfor(j=0;j<c;j++){nscanf(“%dâ€,&b[i][j]);n}n}for(i=0;i<r;i++){nfor(j=0;j<c;j++){nprintf(“%d “,a[i][j]+b[i][j]);n}printf(“nâ€);n}nreturn 0;n}n
Time complexity: O(n^2)
Recommended Programs