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