fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 8 P. 487 #3
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Compute Lottery Winners
  6.  * ____________________________________________________________________________
  7.  * This program contains a lottery ticket buyer's same 10 5-dgit lucky
  8.  * combinations used when they buy 10 tickets a week. The program will display
  9.  * whether or not this week's winning 5-digit number matches with any of the
  10.  * lottery ticket buyer's lucky combinations.
  11.  * ____________________________________________________________________________
  12.  * Input
  13.  * SIZE : size of the array containing the lucky combinations
  14.  * tickets : array containing the 10 lucky combinations
  15.  * winningNumber : this week's winning number
  16.  * Output
  17.  * isWinner : true/false of whether or not the lucky combinations match
  18.  * the winning number
  19.  *****************************************************************************/
  20. #include <iostream>
  21. #include <iomanip>
  22. using namespace std;
  23. //Function Prototypes
  24. void bubbleSort(int arr[], int size);
  25. bool binarySearch(const int arr[], int size, int value);
  26.  
  27. int main() {
  28. //Data Dictionary
  29. const int SIZE = 10;
  30. int tickets[SIZE] = {13579, 26791, 26792, 33445, 55555,
  31. 62483, 77777, 79422, 85647, 93121};
  32. int winningNumber;
  33. bool isWinner = false;
  34.  
  35. //User Input
  36. cout << "Enter this week's 5-digit winning number: " << endl;
  37. cin >> winningNumber;
  38.  
  39. //Input Validation
  40. if(winningNumber < 10000 || winningNumber > 99999){
  41. cout << "Error: Please enter a valid 5-digit number." << endl;
  42. cin >> winningNumber;
  43. return 1;
  44. }
  45. bubbleSort(tickets, SIZE);
  46.  
  47. //Perform Binary Search
  48. isWinner = binarySearch(tickets, SIZE, winningNumber);
  49.  
  50. //Display Result
  51. if(isWinner)
  52. cout << "Congratulations! You have a winning ticket!" << endl;
  53. else
  54. cout << "Sorry, no winning tickets this week." << endl;
  55.  
  56. return 0;
  57. }
  58. //Bubble Sort Function
  59. void bubbleSort(int arr[], int size){
  60. bool swapped;
  61. for(int i = 0; i < size - 1; i++){
  62. swapped = false;
  63. for(int j = 0; j < size - i - 1; j++){
  64. if(arr[j] > arr[j + 1]){
  65. int temp = arr[j];
  66. arr[j] = arr[j + 1];
  67. arr[j + 1] = temp;
  68. swapped = true;
  69. }
  70. }
  71. if(!swapped)
  72. break;
  73. }
  74. }
  75. //Binary Search Function
  76. bool binarySearch(const int arr[], int size, int value){
  77. int first = 0;
  78. int last = size - 1;
  79. int middle;
  80.  
  81. while(first <= last) {
  82. middle = (first + last) / 2;
  83. if(arr[middle] == value)
  84. return true;
  85. else if(arr[middle] > value)
  86. last = middle - 1;
  87. else
  88. first = middle + 1;
  89. }
  90. return false;
  91. }
Success #stdin #stdout 0s 5308KB
stdin
77777
stdout
Enter this week's 5-digit winning number: 
Congratulations! You have a winning ticket!