YouTip LogoYouTip

C Examples Add Matrix

# C Language Example - Adding Two Matrices [![Image 3: C Language Examples](#) C Language Examples](#) Adding two matrices using multidimensional arrays. ## Example #includeint main(){int r, c, a, b, sum, i, j; printf("Enter number of rows ( 1 ~ 100): "); scanf("%d", &r); printf("Enter number of columns ( 1 ~ 100): "); scanf("%d", &c); printf("nEnter elements of the first matrix:n"); for(i=0; i<r; ++i)for(j=0; j<c; ++j){printf("Enter element a%d%d: ",i+1,j+1); scanf("%d",&a); }printf("Enter elements of the second matrix:n"); for(i=0; i<r; ++i)for(j=0; j<c; ++j){printf("Enter element a%d%d: ",i+1, j+1); scanf("%d", &b); }// Adding matrices for(i=0;i<r;++i)for(j=0;j<c;++j){sum=a+b; }// Displaying the result printf("nSum of the two matrices: nn"); for(i=0;i<r;++i)for(j=0;j<c;++j){printf("%d ",sum); if(j==c-1){printf("nn"); }}return 0; } The output is: Enter number of rows ( 1 ~ 100): 2Enter number of columns ( 1 ~ 100): 3Enter elements of the first matrix:Enter element a11: 2Enter element a12: 3Enter element a13: 4Enter element a21: 5Enter element a22: 2Enter element a23: 3Enter elements of the second matrix:Enter element a11: -4Enter element a12: 5Enter element a13: 3Enter element a21: 5Enter element a22: 6Enter element a23: 3Sum of the two matrices: -2 8 7 10 8 6 [![Image 4: C Language Examples](#) C Language Examples](#)
← C Examples Access Array PointeC Examples Array Largest Eleme β†’