fork download
  1. //Roman Lara Delgado CSC5 Chapter 8, P.487, #3
  2. //
  3. /*******************************************************************************
  4. *
  5. * Determine Lottery Winner
  6. *_______________________________________________________________________________
  7. * This program determines if a person, who has bought ten
  8. * lottery tickets, has the winner ticket.
  9. * ______________________________________________________________________________
  10. * INPUT
  11. * ticketWinner : The winner lottery ticket number.
  12. *
  13. * OUTPUT
  14. * A message that says whether the user has won the
  15. * lottery or not.
  16. * ******************************************************************************
  17. * FUNCTION
  18. * ******************************************************************************
  19. * int binarySearch(const int [], int, int)
  20. * ------------------------------------------------------------------------------
  21. * This function locates if a value exists in an array.
  22. *
  23. * PARAMETERS:
  24. * array[] : receives the values of an array.
  25. * size : receives the number of elements
  26. * of the array.
  27. * value : receives a value that the user
  28. * wanted to determine whether the
  29. * value exists in the array or not.
  30. *
  31. *******************************************************************************/
  32. #include <iostream>
  33. using namespace std;
  34.  
  35. int binarySearch(const int [], int, int); //Function prototype
  36. const int SIZE = 10; //GLOBAL - Number of elements of an array.
  37.  
  38. int main () //Start main.
  39. {
  40. int ticketUser[SIZE] = {13579, 26791, 26792, 33445, 55555, 62483,
  41. 77777, 79422, 85647, 93121}; //Tickets of the user.
  42. int ticketWinner; //INPUT - The winner lottery ticket number.
  43. int results; //Hold the position of an array.
  44.  
  45. //Enter the winner lottery ticket number.
  46. cout << "Enter the winner ticket number: " << endl;
  47. cin >> ticketWinner;
  48.  
  49. //Determine the position of the array that holds the user's winner ticket.
  50. results = binarySearch(ticketUser, SIZE, ticketWinner);
  51.  
  52. if(results == -1) //If the array was not found.
  53. {
  54. cout << "Your ticket is not a winner, play again.";
  55. }
  56. else //If the array was found.
  57. {
  58. cout << "YOU WON THE LOTTERY!";
  59. }
  60. return 0;
  61. } //End main.
  62.  
  63. //binarySearch definition
  64. int binarySearch (const int array[], int size, int value)
  65. {
  66. int first = 0, //First array element.
  67. last = size - 1, //Last array element.
  68. middle, //Midpoint of search.
  69. position = -1; //Position of search value.
  70. bool found = false; //Flag.
  71.  
  72. while (!found && first <= last)
  73. {
  74. middle = (first + last) / 2; //Calculate midpoint.
  75.  
  76. if(array[middle] == value) //If value is found at mid.
  77. {
  78. found = true;
  79. position = middle;
  80. }
  81. else if (array[middle] > value) //If value is in lower half.
  82. {
  83. last = middle - 1;
  84. }
  85. else
  86. {
  87. first = middle + 1; //If value is in upper half.
  88. }
  89. }
  90. return position;
  91. } //End binarySearch
Success #stdin #stdout 0s 5304KB
stdin
13452
stdout
Enter the winner ticket number: 
Your ticket is not a winner, play again.