fork download
  1. // Nathanael Schwartz CS1A Chapter 8, P. 515, #1
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * CHECK LOTTERY TICKET NUMBER
  6.  * _____________________________________________________________________________
  7.  * This program stores a player's 10 lottery ticket numbers
  8.  * in an array, prompts the user for the winning number,
  9.  * and then checks if the winning number matches any of the
  10.  * player's tickets using a linear search.
  11.  * _____________________________________________________________________________
  12.  * INPUT
  13.  * winningNumber : The winning 5-digit lottery number
  14.  *
  15.  * OUTPUT
  16.  * Message indicating if the player has a winning ticket
  17.  *
  18.  ******************************************************************************/
  19. #include <iostream>
  20. using namespace std;
  21.  
  22. // Function prototype
  23. bool isWinningTicket(const int[], int, int);
  24.  
  25. int main() {
  26. // List of player's lottery ticket numbers
  27. const int NUM_TICKETS = 10;
  28. int tickets[NUM_TICKETS] = {
  29. 13579, 26791, 26792, 33445, 55555,
  30. 62483, 77777, 79422, 85647, 93121
  31. };
  32.  
  33. int winningNumber; // The winning number for this week
  34.  
  35. // Input: Prompt the user to enter the winning number
  36. cout << "Enter this week's winning 5-digit number: ";
  37. cin >> winningNumber;
  38.  
  39. // Search for the winning number in the player's tickets
  40. if (isWinningTicket(tickets, NUM_TICKETS, winningNumber)) {
  41. cout << "Congratulations! You have a winning ticket!" << endl;
  42. } else {
  43. cout << "Sorry, no winning tickets this week." << endl;
  44. }
  45.  
  46. return 0;
  47. }
  48.  
  49. /**************************************************************
  50.  * isWinningTicket *
  51.  * This function performs a linear search to see if the *
  52.  * winning number matches any of the numbers in the player's *
  53.  * ticket array. Returns true if a match is found; otherwise, *
  54.  * returns false. *
  55.  **************************************************************/
  56. bool isWinningTicket(const int tickets[], int size, int winningNumber) {
  57. for (int i = 0; i < size; i++) {
  58. if (tickets[i] == winningNumber) {
  59. return true; // Winning ticket found
  60. }
  61. }
  62. return false; // No winning ticket found
  63. }
  64.  
Success #stdin #stdout 0.01s 5288KB
stdin
33445
stdout
Enter this week's winning 5-digit number: Congratulations! You have a winning ticket!