fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void){
  5. int a, b;
  6. scanf("%d %d", &a, &b);
  7.  
  8. int **mat;
  9.  
  10. mat = (int **)malloc(sizeof(int *) * a);
  11. if(mat == NULL){
  12. printf("ERROR\n");
  13. return 0;
  14. }
  15.  
  16. for(int i = 0; i < a; i++){
  17. mat[i] = (int *)malloc(sizeof(int) * b);
  18. if(mat[i] == NULL){
  19. printf("ERROR\n");
  20. return 0;
  21. }
  22. }
  23.  
  24.  
  25. int num = 1;
  26. for(int i = 0; i < a; i++){
  27. for(int j = 0; j < b; j++){
  28. mat[i][j] = num++;
  29. }
  30. }
  31.  
  32. // ④ 出力
  33. for(int i = 0; i < a; i++){
  34. for(int j = 0; j < b; j++){
  35. printf("%d", mat[i][j]);
  36. if(j < b - 1) printf(" ");
  37. }
  38. printf("\n");
  39. }
  40.  
  41.  
  42. for(int i = 0; i < a; i++){
  43. free(mat[i]);
  44. }
  45. free(mat);
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 5324KB
stdin
2 3
stdout
1 2 3
4 5 6