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