// Kurt Feiereisel CSC5 Chapter 8, p.487, #3
/*******************************************************************************
*
* Determine if a User Won or Not
* _____________________________________________________________________________
* This program allows a user to enter this weeks winning lottery numbers. The
* function will then determine and report if the user won the lottery or not.
* _____________________________________________________________________________
* INPUT:
* winning : The winning lottery numbers for the week
* OUTPUT:
* Whether the user won the lottery or not
*
* ****************************************************************************/
#include <iostream>
using namespace std;
// Function Prototypes
int inputWinningNum(int winning);
void sort(int puchased[], int SIZE);
void determine(int purchased[], int winning, int SIZE);
int main()
{
// Initalize Variables/Constants/Arrays
int winning = 0;
const int SIZE = 10;
int purchased[SIZE] = {13579, 26791, 26792, 33445, 55555, 62483, 77777,
79422, 85647, 93121};
// Function Call
sort(purchased, SIZE);
winning = inputWinningNum(winning);
determine(purchased, winning, SIZE);
return 0;
}
/*
* Definition of inputWinningNum:
* This function allows a user to enter the winning lottery numbers. This
* function will return the value entered to main.
*/
int inputWinningNum(int num)
{
// Input num
cout << "Please enter the winning lottery numbers: " << endl;
cin >> num;
return num;
}
/*
* Definition of sort Function:
* This function will sort the array purchased into to ascending order.
*/
void sort(int p[], int size)
{
// Declare Local Variables
int scan;
int minIndex;
int minNum;
// Implement For loop to sort Variables (selection sort)
for(scan = 0; scan < (size - 1); scan++)
{
// Initalize Local Variables
minIndex = scan;
minNum = p[scan];
// For loop to sort elements of array
for(int index = scan + 1; index < size; index++)
{
if(p[index] < minNum)
{
minNum = p[index];
minIndex = index;
}
}
p[minIndex] = p[scan];
p[scan] = minNum;
}
}
/*
* Definition of determine Function:
* This function determines and reports whether the user won the lottery
*/
void determine(int p[], int win, int size)
{
int first = 0; // First array element
int last = size - 1; // Last array element
int middle; // Midpoint of search
int position = -1; // Position of search Value
bool found = false; // Flag
// Implement while loop to find winning number
while (!found && first <= last)
{
middle = (first + last) / 2;
if(p[middle] == win)
{
found = true;
position = middle;
}
// Change First and last elements
else if(p[middle] > win)
last = middle - 1;
else
first = middle + 1;
}
if(position == -1)
cout << "You lost!" << endl;
else
cout << "You won the lottery! The winning number was "
<< p[position] << ".\n";
}