fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5. int i,j,k;
  6. int a,b;
  7. int **mat;
  8. scanf("%d %d",&a,&b);
  9.  
  10. //ここで2次元配列の動的確保をする
  11. mat = (int **)malloc(sizeof(int *) * a);
  12. if(mat == NULL){
  13. printf("ERROR\n");
  14. return 0;
  15. }
  16.  
  17. for(i=0;i<a;i++){
  18. mat[i] = (int *)malloc(sizeof(int) * b);
  19. if(mat[i] == NULL){
  20. printf("ERROR\n");
  21. return 0;
  22. }
  23. }
  24. //ここで2次元配列に数値を代入する
  25. k = 1;
  26. for(i=0;i<a;i++){
  27. for(j=0;j<b;j++){
  28. mat[i][j] = k;
  29. k++;
  30. }
  31. }
  32. //以下の部分は表示の部分です
  33. //いじらなくてOK
  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. // 確保した領域をfree関数で解放する
  43. for(i=0;i<a;i++){
  44. free(mat[i]);
  45. }
  46. free(mat);
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 5348KB
stdin
Standard input is empty
stdout
Standard output is empty