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