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