fork download
  1. //Jonathan Estrada CSC5 Chapter 8, P.487, #3
  2. /*******************************************************************************
  3.  * DETERMINE LOTTARY TICKET
  4.  * _____________________________________________________________________________
  5.  * This program will receive a lottary ticket combination from the user and tell
  6.  * them if they won or not.
  7.  * _____________________________________________________________________________
  8.  * INPUT
  9.  * lottoNumbers n : all possible winning numbers
  10.  * SIZE : array size
  11.  * userLottoTicket : user lottary ticket number
  12.  * OUTPUT
  13.  * result : the return value of if it was found
  14.  * *****************************************************************************/
  15. #include <iostream>
  16. using namespace std;
  17.  
  18. int lottoMatchSearch( const int[], int, int);
  19. const int SIZE = 10;
  20.  
  21. int main() {
  22.  
  23. int lottoNumbers[SIZE] = {13579, 26791, 26792, 33445, 55555, 62483, 77777,
  24. 79422, 85647, 93121 };
  25.  
  26. int userLottoTicket;
  27. int result;
  28.  
  29. cout << "Please enter lotto ticket digits: ";
  30. cin >> userLottoTicket;
  31. cout << userLottoTicket << endl;
  32.  
  33. result = lottoMatchSearch(lottoNumbers, SIZE, userLottoTicket);
  34.  
  35. if(result == -1)
  36. cout << "You are not a winner." << endl;
  37. else
  38. cout << "Winning ticket. ";
  39.  
  40. return 0;
  41. }
  42. /*******************************************************************************
  43.  * Definiton of funcion lottoMatchSearch.
  44.  * This function will conduct a linear search using the arrays combindations
  45.  * and see in the user got a match.
  46.  * *****************************************************************************/
  47. int lottoMatchSearch(const int lottoNumbers[], int SIZE, int userLottoTicket)
  48. {
  49. int first = 0;
  50. int last = SIZE - 1;
  51. int middle;
  52. int positon = - 1;
  53. bool found = false;
  54.  
  55. while(!found && first <= last)
  56. {
  57. middle = (first + last)/2;
  58. if(lottoNumbers[middle] == userLottoTicket)
  59. {
  60. found = true;
  61. positon = middle;
  62. }
  63. else if (lottoNumbers[middle]>userLottoTicket)
  64. last = middle - 1;
  65. else
  66. first = middle + 1;
  67. }
  68. return positon;
  69. }
Success #stdin #stdout 0.01s 5276KB
stdin
55555
stdout
Please enter lotto ticket digits: 55555
Winning ticket.