Monday, March 6, 2017

Programming Challenge 8.3 - Lottery Numbers Modification

/* Lottery Numbers Modification - A lottery ticket buyer purchases 10
   tickets a week, always playing the same 10 5-digit "lucky" combinations.
   This program initializes an array with these numbers and then lets the
   player enter this week's winning 5-digit number. The program performs a
   binary search through the list of the player's numbers and report whether
   or not one of the tickets is a winner this week. The numbers are:
  
      13579       26791    26792     33445      55555
      62483       77777    79422     85647      93121 */

#include "Utility.h"

void searchTicketNums(const int[], const int, int);
int winningCombination();

int main()
{
   /* Number of elements in the array */
   const int NUMELS = 10;

   /* Array containing the combinations */
   int lotteryNums[NUMELS] = { 13579, 26791, 26792, 33445, 55555,
                               62483, 77777, 79422, 85647, 93121 };

   int combination = 0;

   combination = winningCombination();
   searchTicketNums(lotteryNums, combination, NUMELS);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: luckyNumber

   This function lets the user enter this week's winning
   5-digit number and returns it.
   ********************************************************** */

int winningCombination()
{
   int combination = 0;

   cout << "\n\t\tLotteria Italia\n\n"
        << "Enter your combination: ";
   cin >> combination;

   return combination;
}

/* **********************************************************
   Definition: searchTicketNums

   This function accepts searchTicketNums, combination, and
   NUMELS as its argument. It performs a binary search on the
   array to check whether combination matches a lucky number.
   If it is a winning combination, the user is congratulated
   being the grand-prize winner. If no match was discovered,
   the user is informed accordingly.
   ********************************************************** */

void searchTicketNums(const int lotteryNums[], int winningCombination,
                      int NUMELS)
{
   /* Variables: First array element, Last array element, Midpoint of
                 search, Position of search value */
   int firstElem = 0,
       lastElem = NUMELS - 1,
       midpoint = 0,
       position = - 1;

   /* Flag to indicate a match */
   bool isFound = false;

   while (!isFound && firstElem <= lastElem)
   {
      /* Calculate the midpoint */
      midpoint = (firstElem + lastElem) / 2;

      /* If the arrays midpoint contains the winning combination, isFound is set to
         true and position is set to midpoint. Else if lotteryNums midpoint is greater
         than winningCombination, lastElement is set as new midpoint, otherwise if the
         number is smaller, firstElem is set as the new midpoint to perform the next
         iteration of the search. */
      lotteryNums[midpoint] == winningCombination ? isFound = true, position = midpoint :
      lotteryNums[midpoint] > winningCombination ? lastElem = midpoint - 1:
                                                     firstElem = midpoint + 1;

   }

   isFound ? cout << "\nCONGRATULATIONS! YOU WON EURO 12,000.000!" :
             cout << "\nSorry, this is no winning combination ...";
}

Example Output: 








No comments:

Post a Comment