// Andres Guzman CSC5 Chapter 8, P. 487, #3
/******************************************************************************
*
* DETERMINE LOTTERY STATUS
* ____________________________________________________________________________
* This program will determine whether the inputed numbers for a lottery ticket
* match those of the winning numbers.
*
* Computation:
*
* Uses a binary search in the searchlist function to compare the values in the
* subscripts to the inputed values of the user. If equal, returns a positive
* status, and a negative status otherwise.
* ____________________________________________________________________________
*
* INPUT
* SIZE = Array Size
* lotterywins = The winning numbers for the lottery ticket
* lotterynum = Holds the inputed value for the lottery ticket number
*
* OUTPUT
* status = Displays the status of the lottery ticket
*
******************************************************************************/
#include <iostream>
using namespace std;
int searchlist(const int a[], int b, int c); //Function Prototype
const int SIZE = 10; //Array Size
int main()
{
//
//Initialization List
int lotterywins[SIZE] = {13579, 26791, 26792, 33445, 55555, 62483, 77777,
79422, 85647, 93121};
int lotterynum; //Holds Input Value for users Lottery Numbers
int status; //Holds Status of the lottery Ticket
//
//Prompt Input Display
cout << "Please enter this weeks winning lottery number: ";
cin >> lotterynum;
//
//Input Validation
while(lotterynum < 0)
{
cout << "Cannot Be Negatives. Please Try Again." << endl;
cout << "Please enter this weeks winning lottery number: ";
cin >> lotterynum;
}
//
//Adds Function Results to Variable Status
status = searchlist(lotterywins, SIZE, lotterynum);
//
//Determines whether the inputed values are equal to the subscript
if(status == -1)
cout << "\nTicket is not a winner this week." << endl;
else
cout << "\nTicket is a winner this week!" << endl;
return 0;
}
/*******************************************************************************
* Adding Definition to the function. This function will portion the range into
* halves and determine whether the value inside those halves matches the
* inputed value of the user, in which case will then determine the status of
* the lottery ticket by using a binary search.
******************************************************************************/
int searchlist(const int a[], int b, int c)
{
int first = 0;
int last = b - 1;
int middle;
int position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if(a[middle] == c)
{
found = true;
position = middle;
}
else if(a[middle] > c)
last = middle - 1;
else
first = middle + 1;
}
return position;
}