fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int *foo(size_t row, size_t col);
  5.  
  6. int main(void){
  7. int *arr;
  8. unsigned int row, col, k;
  9.  
  10. printf("Give the ROW: ");
  11. if ( scanf("%u",&row) != 1){
  12. printf("Error, scanf ROW\n");
  13. exit(1);
  14. }
  15.  
  16. printf("Give the COL: ");
  17. if ( scanf("%u",&col) != 1){
  18. printf("Error, scanf COL\n");
  19. exit(2);
  20. }
  21.  
  22. arr = foo(row, col);
  23. for (k = 0 ; k < (row * col) ; k++){
  24. printf("%d ",arr[k]);
  25. }
  26.  
  27. free(arr);
  28. }
  29.  
  30. int *foo(size_t row, size_t col){
  31. unsigned int i, j;
  32. int *arr = malloc(sizeof *arr * row * col);
  33. int l = 0;
  34.  
  35. if(arr == NULL){
  36. printf("Error, malloc\n");
  37. exit(3);
  38. }
  39.  
  40. for ( i = 0; i < row ; i++){
  41. for ( j = 0 ; j < col ; j++){
  42. arr[i * col + j] = l;
  43. l++;
  44. }
  45. }
  46.  
  47. return arr;
  48. }
Success #stdin #stdout 0s 2296KB
stdin
2 5
stdout
Give the ROW: Give the COL: 0 1 2 3 4 5 6 7 8 9