fork download
  1. //Andrew Alspaugh CS1A Chapter 8. P. 487. # 4
  2.  
  3. //Check For Lottery Winner
  4. /****************************************************************************
  5. This program accepts a user input as the winning lottery number. Then the
  6. program checks the array of ticket numbers to check for the winner.
  7. (UPDATED FROM CHALLENGE 2)
  8. ____________________________________________________________________________
  9. //DATA DICTIONARY
  10. //Inputs
  11. const int SIZE = 10;
  12. int luckyWinners[SIZE] = {13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121};
  13.  
  14. //User Input
  15. int Winner;
  16.  
  17. //Bubble Sort
  18. int temp;
  19. bool swap;
  20.  
  21. //Binary Search
  22. int first = 0;
  23. int last = SIZE - 1;
  24. int middle;
  25. int position = -1;
  26. bool found = false;
  27.  
  28. //Outputs
  29. bool valid = true;
  30. *****************************************************************************/
  31. #include <iostream>
  32. using namespace std;
  33.  
  34. int main()
  35. {
  36. //DATA DICTIONARY
  37. //Inputs
  38. const int SIZE = 10;
  39. int luckyWinners[SIZE] = {13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121};
  40.  
  41. //User Input
  42. int Winner;
  43.  
  44. //Bubble Sort
  45. int temp;
  46. bool swap;
  47.  
  48. //Binary Search
  49. int first = 0;
  50. int last = SIZE - 1;
  51. int middle;
  52. int position = -1;
  53. bool found = false;
  54.  
  55. //Outputs
  56. bool valid = true;
  57.  
  58. //INPUT
  59. cout << "Enter This Weeks Winning 5-Digit Number: " << endl;
  60. cin >> Winner;
  61.  
  62. //PROCESS
  63. //bubble sort
  64. do
  65. {
  66. swap = false;
  67. for (int count = 0; count < (SIZE - 1); count++)
  68. {
  69. if (luckyWinners[count] > luckyWinners[count + 1])
  70. {
  71. temp = luckyWinners[count];
  72. luckyWinners[count] = luckyWinners[count + 1];
  73. luckyWinners[count + 1] = temp;
  74. swap = true;
  75. }
  76. }
  77. } while (swap);
  78.  
  79. //binary search
  80. while(!found && first <= last)
  81. {
  82. middle = (first + last / 2);
  83. if (luckyWinners[middle] == Winner)
  84. {
  85. found = true;
  86. position = middle;
  87. }
  88. else if (luckyWinners[middle] > Winner)
  89. last = middle - 1;
  90. else
  91. first = middle + 1;
  92. }
  93.  
  94. //OUTPUT
  95. if (found)
  96. cout << "Congrats you've won" << endl;
  97. else
  98. cout << "Better luck next time" << endl;
  99.  
  100. return 0;
  101. }
Success #stdin #stdout 0.01s 5280KB
stdin
13578
stdout
Enter This Weeks Winning 5-Digit Number: 
Better luck next time