fork download
  1. //Amari Mosley CSC5 Chapter 8, P. 487, #2
  2.  
  3. /*******************************************************************************
  4. *
  5. * LINEAR SEARCH LOTTERY WINNERS
  6. * ______________________________________________________________________________
  7. *
  8. * This program initializes an array with a lottery ticket buyer's
  9. * 10 lucky 5-digit combinations, lets the player enter this week's
  10. * winning 5-digit number, and performs a linear search to report
  11. * whether or not one of the tickets is a winner.
  12. *
  13. *
  14. *_______________________________________________________________________________
  15. *
  16. *
  17. * INPUT
  18. * winningNumber : The 5-digit winning number entered by the user
  19. * OUTPUT
  20. * resultMessage : Indicates whether the ticket is a winner or not
  21. *
  22. *
  23. *******************************************************************************/
  24. #include <iostream>
  25. using namespace std;
  26. int main ()
  27. {
  28. // Constants
  29. const int NUM_TICKETS = 10;
  30.  
  31. // INPUT
  32. int winningNumber;
  33.  
  34. // Initialize Program variables
  35. int tickets[NUM_TICKETS] = { 13579, 26791, 26792, 33445, 55555,
  36. 62483, 77777, 79422, 85647, 93121 };
  37. bool found = false;
  38.  
  39. // Get this week's winning 5-digit number from the user
  40. cout << "Enter this week's 5-digit winning number: ";
  41. cin >> winningNumber;
  42.  
  43. // Search the array for the winning number
  44. for (int index = 0; index < NUM_TICKETS; index++)
  45. {
  46. if (tickets[index] == winningNumber)
  47. {
  48. found = true;
  49. break;
  50. }
  51. }
  52.  
  53. // Output Result
  54. if (found)
  55. {
  56. cout << "Congratulations! You have a winning ticket!" << endl;
  57. }
  58. else
  59. {
  60. cout << "Sorry, none of your tickets are winners this week." << endl;
  61. }
  62.  
  63. return 0;
  64. }
Success #stdin #stdout 0.01s 5348KB
stdin
13579
stdout
Enter this week's 5-digit winning number: Congratulations! You have a winning ticket!