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