// Nathanael Schwartz CS1A Chapter 8, P. 515, #1
//
/*******************************************************************************
*
* CHECK LOTTERY TICKET NUMBER
* _____________________________________________________________________________
* This program stores a player's 10 lottery ticket numbers
* in an array, prompts the user for the winning number,
* and then checks if the winning number matches any of the
* player's tickets using a linear search.
* _____________________________________________________________________________
* INPUT
* winningNumber : The winning 5-digit lottery number
*
* OUTPUT
* Message indicating if the player has a winning ticket
*
******************************************************************************/
#include <iostream>
using namespace std;
// Function prototype
bool isWinningTicket(const int[], int, int);
int main() {
// List of player's lottery ticket numbers
const int NUM_TICKETS = 10;
int tickets[NUM_TICKETS] = {
13579, 26791, 26792, 33445, 55555,
62483, 77777, 79422, 85647, 93121
};
int winningNumber; // The winning number for this week
// Input: Prompt the user to enter the winning number
cout << "Enter this week's winning 5-digit number: ";
cin >> winningNumber;
// Search for the winning number in the player's tickets
if (isWinningTicket(tickets, NUM_TICKETS, winningNumber)) {
cout << "Congratulations! You have a winning ticket!" << endl;
} else {
cout << "Sorry, no winning tickets this week." << endl;
}
return 0;
}
/**************************************************************
* isWinningTicket *
* This function performs a linear search to see if the *
* winning number matches any of the numbers in the player's *
* ticket array. Returns true if a match is found; otherwise, *
* returns false. *
**************************************************************/
bool isWinningTicket(const int tickets[], int size, int winningNumber) {
for (int i = 0; i < size; i++) {
if (tickets[i] == winningNumber) {
return true; // Winning ticket found
}
}
return false; // No winning ticket found
}