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.  
  12. mat = (int **)malloc(a * sizeof(int *));
  13. for(i = 0; i < a; i++){
  14. mat[i] = (int *)malloc(b * sizeof(int));
  15. }
  16.  
  17.  
  18. //ここで2次元配列に数値を代入する
  19.  
  20. for(i = 0; i < a; i++){
  21. for(j = 0; j < b; j++){
  22. mat[i][j] = k++;
  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. free(mat);
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0s 5324KB
stdin
2 3
stdout
1 2 3 
4 5 6