fork download
  1. //Roman Lara Delgado CSC5 Chapter 8, P.487, #2
  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. * Indication if lottery ticket is a winner or loser.
  15. * ******************************************************************************
  16. * FUNCTION
  17. * ******************************************************************************
  18. * int searchList(const int [], int, int)
  19. * ------------------------------------------------------------------------------
  20. * This function locates if a value exists in an array.
  21. *
  22. * PARAMETERS:
  23. * array[] : receives the values of an array.
  24. * size : receives the number of elements
  25. * of the array.
  26. * value : receives a value that the user
  27. * wanted to determine whether the
  28. * value exists in the array or not.
  29. *
  30. *******************************************************************************/
  31. #include <iostream>
  32. using namespace std;
  33.  
  34. int searchList(const int [], int, int); //Function prototype
  35. const int SIZE = 10; //GLOBAL - Number of elements of an array.
  36.  
  37. int main () //Start main.
  38. {
  39. int ticketUser[SIZE] = {13579, 62483, 26791, 77777, 26792, 79422,
  40. 33445, 85647, 55555, 93121}; //Tickets of the user.
  41. int ticketWinner; //INPUT - The winner lottery ticket number.
  42. int results; //Hold the position of an array.
  43.  
  44. //Enter the winner lottery ticket number.
  45. cout << "Enter the winner ticket number: " << endl;
  46. cin >> ticketWinner;
  47.  
  48. //Determine the position of the array that holds the user's winner ticket.
  49. results = searchList(ticketUser, SIZE, ticketWinner);
  50.  
  51. if(results == -1) //If the array was not found
  52. {
  53. cout << "Your ticket is not a winner, play again.";
  54. }
  55. else //If the array was found.
  56. {
  57. cout << "YOU WON THE LOTTERY!";
  58. }
  59. return 0;
  60. } //End main.
  61.  
  62. //searchList defintion
  63. int searchList(const int ticketUser[], int numElems, int winner)
  64. {
  65. int index = 0; //Subscript of the array.
  66. int position = -1; //To record position of search value.
  67. bool found = false; //Flag to indicate if the value was found.
  68.  
  69. while (index < numElems && !found)
  70. {
  71. if (ticketUser[index] == winner) //If the value is found.
  72. {
  73. found = true; //Set the flag.
  74. position = index; //Record the value's subscript.
  75. }
  76. index++; //Go to the next element.
  77. }
  78. return position; //Return the position, or -1.
  79. } //End searchList
  80.  
Success #stdin #stdout 0.01s 5280KB
stdin
12542
stdout
Enter the winner ticket number: 
Your ticket is not a winner, play again.