fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5. int i,j,k;
  6. int a,b;
  7. int **mat;
  8. scanf("%d %d",&a,&b);
  9.  
  10. mat=(int**)malloc(sizeof(int*)*a);
  11. if(mat==NULL){
  12. printf("ERROR\n");
  13. return 0;
  14. }
  15. for(i=0;i<a;i++){
  16. mat[i]=(int*)malloc(sizeof(int)*b);
  17. if(mat[i]==NULL){
  18. printf("ERROR\n");
  19. for(j=0;j<i;j++){
  20. free(mat[j]);
  21. }
  22. free(mat);
  23. return 0;
  24. }
  25. }
  26.  
  27. k=1;
  28. for(i=0;i<a;i++){
  29. for(j=0;j<b;j++){
  30. mat[i][j]=k;
  31. k++;
  32. }
  33. }
  34.  
  35. //以下の部分は表示の部分です
  36. //いじらなくてOK
  37. for(i=0;i<a;i++){
  38. for(j=0;j<b;j++){
  39. printf("%d ",mat[i][j]);
  40. }
  41. printf("\n");
  42. }
  43. for(i=0;i<a;i++){
  44. free(mat[i]);
  45. }
  46. free(mat);
  47.  
  48.  
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 5348KB
stdin
2 3
stdout
1 2 3 
4 5 6