fork download
  1.  
  2. // #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main() {
  6. int a, b;
  7. int i, j, k = 1;
  8. int **mat;
  9. printf("行数と列数を入力してください: ");
  10. scanf("%d %d", &a, &b);
  11.  
  12. mat = malloc(sizeof(int*) * a);
  13. if (mat == NULL) {
  14. printf("メモリ確保に失敗しました\n");
  15. return 1;
  16. }
  17.  
  18.  
  19. for (i = 0; i < a; i++) {
  20. mat[i] = malloc(sizeof(int) * b);
  21. if (mat[i] == NULL) {
  22. printf("メモリ確保に失敗しました\n");
  23. return 1;
  24. }
  25. }
  26.  
  27.  
  28. for (i = 0; i < a; i++) {
  29. for (j = 0; j < b; j++) {
  30. mat[i][j] = k++;
  31. }
  32. }
  33.  
  34. for (i = 0; i < a; i++) {
  35. for (j = 0; j < b; j++) {
  36. printf("%d ", mat[i][j]);
  37. }
  38. printf("\n");
  39. }
  40.  
  41.  
  42. for (i = 0; i < a; i++) {
  43. free(mat[i]);
  44. }
  45. free(mat);
  46.  
  47. return 0;
  48. }
  49.  
  50.  
Success #stdin #stdout 0s 5284KB
stdin
3 2
stdout
行数と列数を入力してください: 1 2 
3 4 
5 6