Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview For this assignment, write a program that will simulate a lottery. Processing The main function for this program is pretty basic. It should start

Overview

For this assignment, write a program that will simulate a lottery.

Processing

The main function for this program is pretty basic. It should start by creating two arrays that can hold a maximum of 5 integer elements. One of the arrays will hold the winning lottery numbers, while the other will hold the users selected lottery numbers.

As with previous programs, the random number generator will be used. So make sure to seed the random number generator by using srand in main().

Call the getWinners function that is described below to fill the array of winning lottery numbers.

Display a title such as "Pick 5 Lottery".

Call the getChoices function that is described below to fill the array of user selected lottery numbers.

Call the sortArray function two times. One call should sort the array of winning numbers. The other call should should sort the array of user selected numbers.

Finally, call the printWinners function that is described below to display the results of the lottery drawing.

The Functions

For this program, two functions have been provided. They can be found here: assign7.cpp

They functions are:

bool isDuplicate( int array[], int arraySize, int searchNum )

This function will check to see if an integer value has already been placed into an array.

It takes three arguments: the array of integers to be searched, the number of values in the array of integers, and the integer value to search for in the array of integers.

It returns a boolean value of true if searchNum is found in array or a boolean value of false if searchNum is not in found in array.

int getUserNum( int upperBd )

This function will get a number between 1 and an upperbound, inclusive.

It takes one argument: the integer upperbound.

It returns an integer: the user selected value that is between 1 and the passed in upperbound.

Note: before calling this function, execute a cout statement to display a prompt to the user. For example:

int num; cout << "User Choice: "; num = getUserNum( MAX_NUM ); //where MAX_NUM is a symbolic constant 

The following functions are the ones that you are required to write for the program:

void getWinners( int array[], int numValues )

This function will generate the winning lottery numbers and place them into an array. There will be numValues number of unique winning numbers.

It takes two arguments: an array of integers that will be filled with the winning lottery numbers and an integer that represents the number of values to place in the array. It returns nothing.

This function will take advantage of the random number generator to generate the winning lottery numbers, which should be in the range of 1 through 10, inclusive. Use a symbolic constant for the upper bound of 10.

In a loop that executes exactly numValues number of times, generate a random number between 1 and 10. Check to see if the random number has already been placed into the array by using the isDuplicate function that has been provided. This check should be done in a loop. Once a unique random number has been generated place it into the array.

void getChoices( int array[], int numValues )

This function will fill an array with the user's selected lottery numbers. There will be numValues number of unique user selected numbers.

It takes two arguments: an array of integers that will be filled with the user's numbers and an integer that represents the number of values to place in the array. It returns nothing.

This function will be very similar to the getWinners function. However, rather than using the random number generator, it will ask the user to enter the numbers.

In a loop that executes exactly numValues number of times, display a prompt to the user to enter a number and get the number by calling the getUserNum function that has been provided. Check to see if the user' number has already been placed into the array by using the isDuplicate function that has been provided. This check should be done in a loop. Once a unique number has been entered place it into the array.

void sortArray( int array[], int numValues )

This function will use the selection sort algorithm that was presented in lecture to sort the array in ASCENDING order.

This function takes two arguments: the first argument is an array of integers that holds the numbers to be sorted, and the second argument is an integer that holds the number of values to be sorted.

The function returns nothing.

void printWinners( int winArray[], int userArray[], int numValues )

This function will display the winning lottery numbers, how many numbers the user matched, and the amount of money that the user won.

It takes three arguments: the first an array of integers that holds the winning lottery numbers, the second an array of integers that holds the user selected lottery numbers, and the third argument is an integer that holds the number of values in the arrays. The function returns nothing.

Create a local array of 6 doubles to hold the amount of money that is won for a specific number of matches. It should be initialized with the following prize amounts: $0 for 0 matches, $1 for 1 match, $5 for 2 matches, $10 for 3 matches, $20 for 4 matches, and $25 for 5 matches.

Display a title such as "The winning numbers are." Follow this with a loop that executes numValues number of times and displays the numbers in the array of winning numbers.

Next, check to see how many numbers the user selected match with the winning numbers. This should be done by using nested loops that each execute numValues number of times. The outer loop will be controlled by the subscript for the array of user selected numbers, while the inner loop will be controlled by the subscript for the array of winning numbers. In the inner loop, compare a value from the array of user selected numbers with a value from the array of winning number. If a match is found, increment a counter of the number of matches.

Finally, display the number of matches that were found and how much money the user won. If the array of prize amounts was created correctly, the number matches can be used as a subscript to get a prize amount from the array.

The prize amount should be displayed with a leading dollar sign and exactly 2 digits after the decimal point.

Symbolic Constants

This program requires the use of 2 symbolic constants.

The first constant should represent the maximum number of picks that the user can make. The value should be 5.

The second constant should represent the maximum possible lottery number. The value should be 10.

Programming Requirements:

Both of the symbolic constants must be used in the program.

Add #include and #include at the top of the program.

Make sure to seed the random number generator by calling the srand function with a value of time(0). This can be done in main before calling the getWinners function.

The modification should be made in main by displaying a menu to the user and getting their choice. This new value should be passed as the second argument for the getWinners and getChoices functions, and the third argument for the printWinners function. It should also be used in the title that is displayed before the user makes their choice.

Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don't take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.

Output

As noted with the other set of output, the values will probably be different.

Run 1

Lottery Game 3. Pick 3 4. Pick 4 5. Pick 5 Which game would you like to play? 3 Pick 3 lottery User choice 1: 7 User choice 2: 4 User choice 3: 9 The winning numbers are 4 5 9 You matched 2 number(s). You win $5.00 

Run 2

Lottery Game 3. Pick 3 4. Pick 4 5. Pick 5 Which game would you like to play? 5 Pick 5 lottery User choice 1: 7 User choice 2: 4 User choice 3: 8 User choice 4: 1 User choice 5: 3 The winning numbers are 1 3 4 7 8 You matched 5 number(s). You win $25.00 main ccp is 

#include #include #include #include

using namespace std;

//function prototypes

bool isDuplicate( int [], int, int ); int getUserNum( int );

int main() {

return 0; }

/****************************************************************** Function: bool isDuplicate( int [], int, int )

Use: This function determines if a value has already been placed into an array

Arguments: int []: the array holding all of the values int: the number of values the array holds int: the value to look for in the array

Returns: a boolean value that represents whether the value was or was not found in the array. true if the value is a duplicate. false if the value is not a duplicate ******************************************************************/

bool isDuplicate( int array[], int arraySize, int searchNum ) { //Loop through the array to determine if it is holding searchNum //If a match is found, return true

for( int sub = 0; sub < arraySize; sub++ ) { if( array[sub] == searchNum ) return true; }

//At this point, no match was found in the array, so false is //returned.

return false; }

/****************************************************************** Function: int getUserNum( int upperBd )

Use: This function gets a number from the user that is between 1 and an upper bound

Arguments: int: the upper bound

Returns: an integer between 1 and the upper bound, inclusive

Note: This function will not display an initial prompt to the user. ******************************************************************/

int getUserNum( int upperBd ) { int userNum;

//Get an integer number from the user

cin >> userNum;

//Validate the number that the user entered. If the number is invalid, //display an error message to the user and get another number. Do this //

while( userNum < 1 or userNum > upperBd ) { cout << "The choice must be between 1 and " << upperBd << ", reenter: "; cin >> userNum; }

//Return the valid value return userNum; }

 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions