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