Add two matrices with C.
Input elements in 3x3 matrix1: 1 2 3 4 5 6 7 8 9 Input elements in 3x3 matrix2: 9 8 7 6 5 4 3 2 1
Sum of both matrix = 10 10 10 10 10 10 10 10 10
#include <stdio.h>int main(){int A[10][10]; // Matrix 1int B[10][10]; // Matrix 2int C[10][10]; // Resultant matrixint row, col;/* Input elements in first matrix*/printf("Enter elements in matrix A of size 3x3: \n");for (row = 0; row < 3; row++){for (col = 0; col < 3; col++){scanf("%d", &A[row][col]);}}/* Input elements in second matrix */printf("\nEnter elements in matrix B of size 3x3: \n");for (row = 0; row < 3; row++){for (col = 0; col < 3; col++){scanf("%d", &B[row][col]);}}/** Add both matrices A and B entry wise or element wise* and stores result in matrix C*/for (row = 0; row < 3; row++){for (col = 0; col < 3; col++){/* Cij = Aij + Bij */C[row][col] = A[row][col] + B[row][col];}}/* Print the value of resultant matrix C */printf("\nSum of matrices A+B = \n");for (row = 0; row < 3; row++){for (col = 0; col < 3; col++){printf("%d ", C[row][col]);}printf("\n");}return 0;}
Output
Enter elements in matrix A of size 3x3:
1 2 3
4 5 6
7 8 9
Enter elements in matrix B of size 3x3:
9 8 7
6 5 4
3 2 1
Sum of matrices A+B =
10 10 10
10 10 10
10 10 10
0 Comments