fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void) {
  5. int rows, cols;
  6. scanf("%d %d", &rows, &cols);
  7.  
  8.  
  9. int **a = malloc(sizeof(int*) * rows);
  10. for (int i = 0; i < rows; i++) {
  11. a[i] = malloc(sizeof(int) * cols);
  12. }
  13.  
  14. int s = 1;
  15.  
  16.  
  17. for (int i = 0; i < rows; i++) {
  18. for (int j = 0; j < cols; j++) {
  19. a[i][j] = s;
  20. s++;
  21. }
  22. }
  23.  
  24.  
  25. for (int i = 0; i < rows; i++) {
  26. for (int j = 0; j < cols; j++) {
  27. printf("%2d ", a[i][j]);
  28. }
  29. printf("\n");
  30. }
  31.  
  32.  
  33. for (int i = 0; i < rows; i++) {
  34. free(a[i]);
  35. }
  36. free(a);
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0.01s 5320KB
stdin
2 3
stdout
 1  2  3 
 4  5  6