//Roman Lara Delgado CSC5 Chapter 8, P.487, #3
//
/*******************************************************************************
*
* Determine Lottery Winner
*_______________________________________________________________________________
* This program determines if a person, who has bought ten
* lottery tickets, has the winner ticket.
* ______________________________________________________________________________
* INPUT
* ticketWinner : The winner lottery ticket number.
*
* OUTPUT
* A message that says whether the user has won the
* lottery or not.
* ******************************************************************************
* FUNCTION
* ******************************************************************************
* int binarySearch(const int [], int, int)
* ------------------------------------------------------------------------------
* This function locates if a value exists in an array.
*
* PARAMETERS:
* array[] : receives the values of an array.
* size : receives the number of elements
* of the array.
* value : receives a value that the user
* wanted to determine whether the
* value exists in the array or not.
*
*******************************************************************************/
#include <iostream>
using namespace std;
int binarySearch(const int [], int, int); //Function prototype
const int SIZE = 10; //GLOBAL - Number of elements of an array.
int main () //Start main.
{
int ticketUser[SIZE] = {13579, 26791, 26792, 33445, 55555, 62483,
77777, 79422, 85647, 93121}; //Tickets of the user.
int ticketWinner; //INPUT - The winner lottery ticket number.
int results; //Hold the position of an array.
//Enter the winner lottery ticket number.
cout << "Enter the winner ticket number: " << endl;
cin >> ticketWinner;
//Determine the position of the array that holds the user's winner ticket.
results = binarySearch(ticketUser, SIZE, ticketWinner);
if(results == -1) //If the array was not found.
{
cout << "Your ticket is not a winner, play again.";
}
else //If the array was found.
{
cout << "YOU WON THE LOTTERY!";
}
return 0;
} //End main.
//binarySearch definition
int binarySearch (const int array[], int size, int value)
{
int first = 0, //First array element.
last = size - 1, //Last array element.
middle, //Midpoint of search.
position = -1; //Position of search value.
bool found = false; //Flag.
while (!found && first <= last)
{
middle = (first + last) / 2; //Calculate midpoint.
if(array[middle] == value) //If value is found at mid.
{
found = true;
position = middle;
}
else if (array[middle] > value) //If value is in lower half.
{
last = middle - 1;
}
else
{
first = middle + 1; //If value is in upper half.
}
}
return position;
} //End binarySearch