Monday, March 6, 2017

Programming Challenge 8.2 - Lottery Winners

/* Lottery Winners - 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 linear 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 linear 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)
{
   int index = 0;

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

   while (index < NUMELS && !isFound)
   {
      /* If it is a winning combination, isFound is set to true and
         the loop exits, otherwise lotteryNums is searched to the end
         and isFound is set to false */
      lotteryNums[index] == winningCombination ? isFound = true :
                                                 isFound = false;

      index++;
   }

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

Example Output:




No comments:

Post a Comment