#include <stdio.h>
#define N 3 // size of the matrix
int main() {
int i, j;
int A[N][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int B[N][N] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int C[N][N]; // result matrix
// perform matrix addition
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
// print the result matrix
printf("Result matrix:\n");
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
This program defines a 3x3 matrix
A
and a 3x3 matrix B
, and performs their addition to produce a new 3x3 matrix C
. It then prints the resulting matrix to the console.When you compile and run this program, it should print the following output:
Result matrix:
10 10 10
10 10 10
10 10 10
10 10 10
10 10 10
10 10 10
This is the result of adding the two matrices
A
and B
element-wise.
0 Comments