#include <stdio.h> #include <stdlib.h>
int main() { int rows = 3, cols = 3; int **a, **b, **c;
a = (int **)malloc(rows * sizeof(int *));
b = (int **)malloc(rows * sizeof(int *));
c = (int **)malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++) {
a[i] = (int *)malloc(cols * sizeof(int));
b[i] = (int *)malloc(cols * sizeof(int));
c[i] = (int *)malloc(cols * sizeof(int));
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
a[i][j] = i * cols + j + 1;
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
b[i][j] = 3;
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
printf("Matrix a:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("Matrix b:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", b[i][j]);
}
printf("\n");
}
printf("\nMatrix c = a+b:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
for (int i = 0; i < rows; i++) {
free(a[i]);
free(b[i]);
free(c[i]);
}
free(a);
free(b);
free(c);
return 0;
}