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.  
  9. scanf("%d %d",&a,&b);
  10.  
  11. mat = (int**)malloc(sizeof(int*)*a);
  12. for(i=0;i<a;i++){
  13. mat[i] = (int*)malloc(sizeof(int)*b);
  14. }
  15.  
  16. for(i=0;i<a;i++){
  17. for(j=0;j<b;j++){
  18. mat[i][j] = k;
  19. k++;
  20. printf("%d ",mat[i][j]);
  21. }
  22. printf("\n");
  23. }
  24.  
  25. for(i=0;i<a;i++){
  26. free(mat[i]);
  27. }
  28. free(mat);
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 5308KB
stdin
4 4
stdout
1 2 3 4 
5 6 7 8 
9 10 11 12 
13 14 15 16