fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int main(){
  6.  
  7. int i,j,k=1;
  8. int a,b;
  9. int **mat;
  10. scanf("%d %d",&a,&b);
  11. mat=(int**)malloc(sizeof(int*)*a);
  12. if(mat==NULL){
  13. printf("erroe\n");
  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. return 0;
  20. }
  21. }
  22. for(i=0;i<a;i++){
  23. for(j=0;j<b;j++){
  24. mat[i][j]=k;
  25. k++;
  26. }
  27. }
  28.  
  29. for(i=0;i<a;i++){
  30. for(j=0;j<b;j++){
  31. printf("%d ",mat[i][j]);
  32. }
  33. printf("\n");
  34. }
  35.  
  36. for(i=0;i<a;i++){
  37. free(mat[i]);
  38. }
  39. free(mat);
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5288KB
stdin
2 3
stdout
1 2 3 
4 5 6