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