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.  
  11. scanf("%d %d",&a,&b);
  12.  
  13. //ここで2次元配列の動的確保をする
  14.  
  15. mat = (int **)malloc(sizeof(int *) * a);
  16.  
  17. if(mat == NULL){
  18. printf("ERROR\n");
  19. return 0;
  20. }
  21.  
  22. for(i=0;i<a;i++){
  23. mat[i] = (int *)malloc(sizeof(int) * b);
  24.  
  25. if(mat[i] == NULL){
  26. printf("ERROR\n");
  27. return 0;
  28. }
  29. }
  30.  
  31.  
  32. //ここで2次元配列に数値を代入する
  33. k = 1;
  34.  
  35. for(i=0;i<a;i++){
  36. for(j=0;j<b;j++){
  37. mat[i][j] = k;
  38. k++;
  39. }
  40. }
  41.  
  42.  
  43. //以下の部分は表示の部分です
  44. //いじらなくてOK
  45. for(i=0;i<a;i++){
  46. for(j=0;j<b;j++){
  47. printf("%d ",mat[i][j]);
  48. }
  49. printf("\n");
  50. }
  51.  
  52.  
  53. //最後に動的確保した領域を解放
  54. for(i=0;i<a;i++){
  55. free(mat[i]);
  56. }
  57.  
  58. free(mat);
  59.  
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0s 5320KB
stdin
2 3
stdout
1 2 3 
4 5 6