fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define SEATS_PER_ROW 20
  5. #define ROWS 10
  6.  
  7. main()
  8. {
  9. int i, j, k, max;
  10. int arr[ROWS][SEATS_PER_ROW]= {{0}};
  11.  
  12. for(i=0; i<10; i++)
  13. {
  14. for(j=0; j<20; j++)
  15. {
  16. arr[i][j] = 0;
  17. }
  18. }
  19.  
  20. for(i=0; i<10; i++)
  21. {
  22. for(j=0; j<20; j++)
  23. {
  24. printf("%d", arr[i][j]);
  25. }
  26. printf("\n");
  27. }
  28. while(1)
  29. {
  30. printf("Please enter the number of seats needed\n");
  31.  
  32. int num, n=0;
  33. if (1 != scanf("%d",&num))
  34. {
  35. printf("Illegal input - stop program\n");
  36. break;
  37. }
  38. printf("Searching for %d seats\n", num);
  39. if (num > SEATS_PER_ROW)
  40. {
  41. printf("We don't have that many seats in a single row\n");
  42. continue;
  43. }
  44.  
  45. int found = 0;
  46. for (i=0; i<10 && found != 1; i++)
  47. {
  48. found = 0;
  49. for (j=0; j<(20-num+1) && found != 1; j++)
  50. {
  51. found = 0;
  52. // Check for num consecutive free seats starting at i, j
  53. for (k=0; k<num; k++)
  54. {
  55. if(arr[i][j+k] != 0)
  56. {
  57. // Can't sit here
  58. found = -1;
  59. break;
  60. }
  61. }
  62. if (found == 0)
  63. {
  64. // We found a place - starting at i, j
  65. // Mark them as used
  66. found = 1;
  67. printf("Seats assinged in row %d : seat %d to %d\n", i, j, j+num-1);
  68. for(k=0; k<num; k++)
  69. {
  70. arr[i][j+k] = 1;
  71. }
  72.  
  73. // Debug print
  74. //for(i=0; i<10; i++)
  75. //{
  76. // for(j=0; j<20; j++)
  77. // {
  78. // printf("%d", arr[i][j]);
  79. // }
  80. // printf("\n");
  81. //}
  82.  
  83. }
  84. }
  85.  
  86. }
  87. if (found != 1)
  88. {
  89. printf("So many seats are not available\n");
  90. }
  91. }
  92. return 0;
  93. }
Success #stdin #stdout 0s 2116KB
stdin
10
15
2
6
20
3
21
9
15
stdout
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
Please enter the number of seats needed
Searching for 10 seats
Seats assinged in row 0 : seat 0 to 9
Please enter the number of seats needed
Searching for 15 seats
Seats assinged in row 1 : seat 0 to 14
Please enter the number of seats needed
Searching for 2 seats
Seats assinged in row 0 : seat 10 to 11
Please enter the number of seats needed
Searching for 6 seats
Seats assinged in row 0 : seat 12 to 17
Please enter the number of seats needed
Searching for 20 seats
Seats assinged in row 2 : seat 0 to 19
Please enter the number of seats needed
Searching for 3 seats
Seats assinged in row 1 : seat 15 to 17
Please enter the number of seats needed
Searching for 21 seats
We don't have that many seats in a single row
Please enter the number of seats needed
Searching for 9 seats
Seats assinged in row 3 : seat 0 to 8
Please enter the number of seats needed
Searching for 15 seats
Seats assinged in row 4 : seat 0 to 14
Please enter the number of seats needed
Illegal input - stop program