//Amari Mosley CSC5 Chapter 8, P. 487, #3
 
/*******************************************************************************
*
* BNARY SEARCH LOTTERY WINNERS
* ______________________________________________________________________________
*
* This program initializes an array with a lottery ticket buyer's 
* 10 lucky 5-digit combinations, lets the player enter this week's 
* winning 5-digit number, and performs a binary search to report 
* whether or not one of the tickets is a winner.
*
*
* ______________________________________________________________________________
*
*
* INPUT
*		winningNumber       : The 5-digit winning number entered by the user
* OUTPUT
*		resultMessage       : Indicates whether the ticket is a winner or not
*
*
********************************************************************************/
#include <iostream>
using namespace std;
int main ()
{
    // Constants
    const int NUM_TICKETS = 10;
    
    // INPUT
    int winningNumber;
    
    // Initialize Program variables
    int tickets[NUM_TICKETS] = { 13579, 26791, 26792, 33445, 55555,
                                 62483, 77777, 79422, 85647, 93121 };
    int first = 0;
    int last = NUM_TICKETS - 1;
    int middle;
    bool found = false;
    
    // Get this week's winning 5-digit number from the user
    cout << "Enter this week's 5-digit winning number: ";
    cin >> winningNumber;
    
    // Search the array for the winning number using binary search
    while (!found && first <= last)
    {
        middle = (first + last) / 2;
        if (tickets[middle] == winningNumber)
        {
            found = true;
        }
        else if (tickets[middle] > winningNumber)
        {
            last = middle - 1;
        }
        else
        {
            first = middle + 1;
        }
    }
    
    // Output Result
    if (found)
    {
        cout << "Congratulations! You have a winning ticket!" << endl;
    }
    else
    {
        cout << "Sorry, none of your tickets are winners this week." << endl;
    }
    
    return 0;
}