BUPT-homework/semester1/pset13/2-2-malloc2d.c
2021-12-10 23:31:54 +08:00

33 lines
637 B
C

#include <stdio.h>
#include <stdlib.h>
int main(void) {
int row, col;
scanf("%d%d", &row, &col);
// Initialize the 2d array
int **arr = malloc(sizeof(int *) * row);
for (int i = 0; i < row; i++) {
arr[i] = malloc(sizeof(int) * col);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
scanf("%d", &arr[i][j]);
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col - 1; j++) {
printf("%d ", arr[i][j] * -10);
}
printf("%d\n", arr[i][col - 1] * -10);
}
// Free the 2d array
for (int i = 0; i < row; i++) {
free(arr[i]);
}
free(arr);
return 0;
}