//Nathan Dominguez CSC5 Chapter 8, P.487, #3
//
/*******************************************************************************
*
* Determine Lottery Winner with Binary Search
* ____________________________________________________________________________
* This prorgam will prompt the user to enter this week's winning 5-digit
* numbers. The program will then use a binary search to validate the numbers
* to the list of numbers given.
* ____________________________________________________________________________
* INPUT
* lottoInput : Lotto number input
*
* OUTPUT
*
*
* CONSTANT
* tickets : The lotto number array
*
*****************************************************************************/
//PROTOTYPE BinSearchNum()
bool BinSearchNum (const int tickets[], int lottoInput);
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
//CONSTANT - The lotto number array
const int tickets[10] = {13579, 26791, 26792, 33445, 55555,
62483, 77777, 79422, 85647, 93121};
int lottoInput; //INPUT - Lotto number input
//INPUT - enter number
cout << "Please Enter Your Lotto Number: ";
cin >> lottoInput;
cout << lottoInput << endl;
//OUTPUT - output if winner or not
if (BinSearchNum(tickets, lottoInput))
{
cout << "You have the winning ticket!";
}
else
{
cout << "You do not have the winning ticket! Sorry!";
}
return 0;
}
//FUNCTION - compare numbers to see if input choice is a winner
bool BinSearchNum (const int tickets[], int lottoInput)
{
int size = 10;
int numFirst = 0;
int numLast = size - 1;
int numMid;
//
//LOOP - sort numbers and put them in order
while (numFirst <= numLast)
{
numMid = (numFirst + numLast) / 2;
if (tickets[numMid] == lottoInput)
{
return true;
}
else if (tickets[numMid] > lottoInput)
{
numLast = numMid - 1;
}
else if (tickets[numMid] < lottoInput)
{
numFirst = numMid + 1;
}
}
return false;
}