//Ava Huntington CS1A Chapter 8 Homework P. 487, #2
//
/*******************************************************************************
* DETERMINE LOTTERY WIN
* _____________________________________________________________________________
* This program will determine if a persons 10 "lucky" combinations for a weekly
* lottery matches the current weeks winning number using a binary search.
* _____________________________________________________________________________
* INPUT
* "lucky" combinations: given "lucky" combinations
* winning ticket: weekly winning lottery ticket
*
* OUTPUT
* win or lose: wether or not lucky combination matches winning #
******************************************************************************/
#include <iostream>
using namespace std;
int searchLotteryBinary(const int[], int, int);
const int SIZE = 10;
int main()
{
int combinations[SIZE] = {13579, 26791, 26792, 33445,
55555, 62483, 77777, 79422, 85647, 93121};
int result; //Matches winning number with lucky combo
int ticketNumber; // Winning number
cout << "*Lottery*" << endl;
cout << "Enter this weeks winning 5 digit number:" << endl;
cin >> ticketNumber;
//Search for account number
result = searchLotteryBinary(combinations, SIZE, ticketNumber);
if(result == -1){
cout << ticketNumber << " is not a winning number, better luck next week!" << endl;
}
else{
cout << ticketNumber << " is a winning number. Congratulations!" << endl;
}
return 0;
}
int searchLotteryBinary(const int list[], int numElms, int value)
{
int first = 0;
int last = numElms -1;
int middle;
int position = -1;
bool found = false;
while(!found && first <= last)
{
middle = (first + last) / 2;
if(list[middle] == value)
{
found = true;
position = middle;
}
else if (list[middle] > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
Ly9BdmEgSHVudGluZ3RvbiAgICAgICAgICAgICBDUzFBICAgICAgICAgICAgICBDaGFwdGVyIDggSG9tZXdvcmsgUC4gNDg3LCAjMgovLwovKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKgogKiBERVRFUk1JTkUgTE9UVEVSWSBXSU4KICogX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KICogVGhpcyBwcm9ncmFtIHdpbGwgZGV0ZXJtaW5lIGlmIGEgcGVyc29ucyAxMCAibHVja3kiIGNvbWJpbmF0aW9ucyBmb3IgYSB3ZWVrbHkKICogbG90dGVyeSBtYXRjaGVzIHRoZSBjdXJyZW50IHdlZWtzIHdpbm5pbmcgbnVtYmVyIHVzaW5nIGEgYmluYXJ5IHNlYXJjaC4KICogX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KICogSU5QVVQKICogICAibHVja3kiIGNvbWJpbmF0aW9uczogICBnaXZlbiAibHVja3kiIGNvbWJpbmF0aW9ucwogKiAgICB3aW5uaW5nIHRpY2tldDogICAgICAgIHdlZWtseSB3aW5uaW5nIGxvdHRlcnkgdGlja2V0CiAqIAogKiBPVVRQVVQKICogICAgd2luIG9yIGxvc2U6ICAgICAgICAgICB3ZXRoZXIgb3Igbm90IGx1Y2t5IGNvbWJpbmF0aW9uIG1hdGNoZXMgd2lubmluZyAjCiAqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiovCiNpbmNsdWRlIDxpb3N0cmVhbT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAogaW50IHNlYXJjaExvdHRlcnlCaW5hcnkoY29uc3QgaW50W10sIGludCwgaW50KTsKIGNvbnN0IGludCBTSVpFID0gMTA7CiAKaW50IG1haW4oKQp7CglpbnQgY29tYmluYXRpb25zW1NJWkVdID0gezEzNTc5LCAgMjY3OTEsIDI2NzkyLCAzMzQ0NSwKCQk1NTU1NSwgNjI0ODMsIDc3Nzc3LCA3OTQyMiwgODU2NDcsIDkzMTIxfTsKCglpbnQgcmVzdWx0OyAgICAgICAgICAgICAgICAgIC8vTWF0Y2hlcyB3aW5uaW5nIG51bWJlciB3aXRoIGx1Y2t5IGNvbWJvCglpbnQgdGlja2V0TnVtYmVyOyAgICAgICAgICAgLy8gV2lubmluZyBudW1iZXIKCQoJY291dCA8PCAiKkxvdHRlc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