fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. //必要があれば変数などを追加してもOKです
  5.  
  6. int main(){
  7. int i,j,k;
  8. int a,b;
  9. int **mat;
  10. scanf("%d %d",&a,&b);
  11. mat = (int **)malloc(sizeof(int *) * a);
  12. if (mat == NULL) { printf("ERROR\n"); return 0; }
  13.  
  14. for (k = 0; k < a; k++) {
  15. mat[k] = (int *)malloc(sizeof(int) * b);
  16. if (mat[k] == NULL) {
  17. printf("ERROR\n");
  18. return 0;
  19. }
  20. }
  21.  
  22.  
  23. //ここで2次元配列の動的確保をする
  24. for (i = 0; i < a; i++) {
  25. for (j = 0; j < b; j++) {
  26. mat[i][j] = i * b + j + 1;
  27. }
  28. }
  29.  
  30. //ここで2次元配列に数値を代入する
  31.  
  32.  
  33.  
  34. //以下の部分は表示の部分です
  35. //いじらなくてOK
  36. for(i=0;i<a;i++){
  37. for(j=0;j<b;j++){
  38. printf("%d ",mat[i][j]);
  39. }
  40. printf("\n");
  41. }
  42.  
  43. //さて,最後に忘れずにすることと言えば?
  44. for (i = 0; i < a; i++) free(mat[i]);
  45. free(mat);
  46.  
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 5324KB
stdin
4 5

stdout
1 2 3 4 5 
6 7 8 9 10 
11 12 13 14 15 
16 17 18 19 20