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