// Torrez, Elaine CS1A Chapter 8 P. 487, #3
/********************************************************************************************
*
* SEARCH LOTTERY WINNER USING BINARY SEARCH
*
* ------------------------------------------------------------------------------------------
* This program determines whether any of a buyer's 10 lottery tickets is a winner.
* The program stores a fixed list of 10 "lucky" 5-digit combinations in a sorted
* integer array. It then asks the user to enter this week's winning 5-digit number
* and uses the binary search algorithm to determine whether any ticket matches.
* If a match is found, the program reports that the player has a winning ticket;
* otherwise, it reports that none of the tickets is a winner this week.
* ------------------------------------------------------------------------------------------
*
* INPUT
* winningNum : This week's winning 5-digit lottery number
*
* OUTPUT
* Message stating whether the player has a winning ticket or not
*
********************************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototype
int binarySearch(const int array[], int size, int value);
int main()
{
const int SIZE = 10; // Number of lottery tickets
int tickets[SIZE] = { // Buyer's 10 "lucky" numbers (sorted)
13579, 26791, 26792, 33445, 55555,
62483, 77777, 79422, 85647, 93121
};
int winningNum; // This week's winning number
int position; // Position returned by binary search
// ----------------------------------------------------
// INPUT: Get this week's winning 5-digit number
// ----------------------------------------------------
cout << "Enter this week's winning 5-digit lottery number: ";
cin >> winningNum;
// Simple input validation for a 5-digit positive number
while (winningNum < 10000 || winningNum > 99999)
{
cout << "ERROR: Enter a VALID 5-digit number (10000 - 99999): ";
cin >> winningNum;
}
// ----------------------------------------------------
// PROCESSING: Binary search through the ticket list
// ----------------------------------------------------
position = binarySearch(tickets, SIZE, winningNum);
cout << endl;
cout << fixed << setprecision(0);
// ----------------------------------------------------
// OUTPUT: Report if any ticket is a winner
// ----------------------------------------------------
if (position != -1)
cout << "Congratulations! One of your tickets is a WINNER." << endl;
else
cout << "Sorry, none of your tickets is a winner this week." << endl;
return 0;
}
/********************************************************************************************
* BINARY SEARCH
* ------------------------------------------------------------------------------------------
* This function performs a binary search on a sorted integer array.
* It returns the index of the value if found, or -1 if the value is not in the array.
********************************************************************************************/
int binarySearch(const int array[], int size, int value)
{
int first = 0; // First array index
int last = size - 1; // Last array index
int middle; // Midpoint index
int position = -1; // Position of the value (if found)
bool found = false; // Flag to indicate if value is found
while (!found && first <= last)
{
middle = (first + last) / 2; // Calculate midpoint
if (array[middle] == value) // If value is found at middle
{
found = true;
position = middle;
}
else if (array[middle] > value) // If value is in lower half
{
last = middle - 1;
}
else // If value is in upper half
{
first = middle + 1;
}
}
return position;
}