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. for(i = 0; i < a; i++) {
  12. mat[i] = (int *)malloc(sizeof(int) * b);//0の時、横にサイズを開ける、どんどん縦分やっていく//
  13. }
  14.  
  15. int count = 1;
  16. for(i = 0; i < a; i++) {
  17. for(j = 0; j < b; j++) {
  18. mat[i][j] = count;//値を入れていく//
  19. count++;
  20. }
  21. }
  22.  
  23. for(i=0;i<a;i++){
  24. for(j=0;j<b;j++){
  25. printf("%d ",mat[i][j]);//行列を出力//
  26. }
  27. printf("\n");
  28. }
  29.  
  30. for(i = 0; i < a; i++) {
  31. free(mat[i]);
  32. }
  33. free(mat);
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 544KB
stdin
4 2
stdout
1 2 
3 4 
5 6 
7 8