fork download
  1. //Ava Huntington CS1A Chapter 8 Homework P. 487, #2
  2. //
  3. /*******************************************************************************
  4.  * DETERMINE LOTTERY WIN
  5.  * _____________________________________________________________________________
  6.  * This program will determine if a persons 10 "lucky" combinations for a weekly
  7.  * lottery matches the current weeks winning number using a binary search.
  8.  * _____________________________________________________________________________
  9.  * INPUT
  10.  * "lucky" combinations: given "lucky" combinations
  11.  * winning ticket: weekly winning lottery ticket
  12.  *
  13.  * OUTPUT
  14.  * win or lose: wether or not lucky combination matches winning #
  15.  ******************************************************************************/
  16. #include <iostream>
  17. using namespace std;
  18.  
  19. int searchLotteryBinary(const int[], int, int);
  20. const int SIZE = 10;
  21.  
  22. int main()
  23. {
  24. int combinations[SIZE] = {13579, 26791, 26792, 33445,
  25. 55555, 62483, 77777, 79422, 85647, 93121};
  26.  
  27. int result; //Matches winning number with lucky combo
  28. int ticketNumber; // Winning number
  29.  
  30. cout << "*Lottery*" << endl;
  31. cout << "Enter this weeks winning 5 digit number:" << endl;
  32. cin >> ticketNumber;
  33.  
  34. //Search for account number
  35. result = searchLotteryBinary(combinations, SIZE, ticketNumber);
  36.  
  37. if(result == -1){
  38. cout << ticketNumber << " is not a winning number, better luck next week!" << endl;
  39. }
  40. else{
  41. cout << ticketNumber << " is a winning number. Congratulations!" << endl;
  42.  
  43. }
  44. return 0;
  45. }
  46.  
  47. int searchLotteryBinary(const int list[], int numElms, int value)
  48. {
  49. int first = 0;
  50. int last = numElms -1;
  51. int middle;
  52. int position = -1;
  53. bool found = false;
  54.  
  55. while(!found && first <= last)
  56. {
  57. middle = (first + last) / 2;
  58. if(list[middle] == value)
  59. {
  60. found = true;
  61. position = middle;
  62. }
  63. else if (list[middle] > value)
  64. last = middle - 1;
  65. else
  66. first = middle + 1;
  67. }
  68. return position;
  69. }
  70.  
  71.  
Success #stdin #stdout 0.01s 5280KB
stdin
77777
stdout
*Lottery*
Enter this weeks winning 5 digit number:
77777 is a winning number. Congratulations!