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