Question: a c++ program code a program that selects 10 random non-repeating positive numbers (the Lucky5 Lottery numbers), inputs five numbers from the user and checks
a c++ program
code a program that selects 10 random non-repeating positive numbers (the Lucky5 Lottery numbers), inputs five numbers from the user and checks the input with the 10 lottery numbers. The lottery numbers range from 1 to 100. The user wins if at least one input number matches the lottery numbers.
Study displaying arrays in functions described here. As you program your project, demonstrate to the lab instructor displaying an array passed to a function. Create a project. Declare an array wins of 10 integer elements.
Define the following functions:
- Define function assign() that takes array wins[] as a parameter and assigns 0 to each element of the array.
Hint: Array elements are assigned 0, which is outside of lottery numbers' range, to distinguish elements that have not been given lottery numbers yet.
Passing an array as a parameter and its initialization is done similar to the code in this program.
- Define a predicate function check() that takes a number and the array wins[] as parameters and returns true if the number matches one of the elements in the array, or false if none of the elements do. That is, in the function, you should write the code that iterates over the array looking for the number passed as the parameter. You may assume that the number that check() is looking for is always positive.
Hint: Looking for the match is similar to looking for the minimum number in this program.
- Define a function draw() that takes array wins as a parameter and fills it with 10 random integers whose values are from 1 to 100. The numbers should not repeat.
Hint: Use srand(), rand() and time() functions that we studied earlier to generate appropriate random numbers from 1 to 100 and fill the array. Before the selected number is entered into the array wins, call function check() to make sure that the new number is not already in the array. If it is already there, select another number.
The pseudocode for your draw() function may be as follows:
declare a variable "number of selected lottery numbers so far", initialize this variable to zero while (the_number_of_selected_elements is less than the array size) select a new random number call check() to see if this new random number is already in the array if the new random number is not in the array increment the number of selected elements add the newly selected element to the array
- Define function entry() that asks the user to enter a single number from 1 to 100 and returns this value.
- Define function printOut() that outputs the selected numbers. Hint: Outputting the array can be done similar to echoing it in this program.
The pseudocode your function main() should be as follows:
main(){ declare array and other variables assign(...) // fill array with 0 draw(...) // select 10 non-repeating random numbers iterate five times entry(...) // get user input use check () to compare user input against lottery numbers if won state and quit printOut(...) // outputs selected lottery numbers } Note: A program that processes each element of the array separately (i.e. accesses all 10 elements of the array for assignment or comparison outside a loop) is inefficient and will result in a poor grade. Likewise, processing each user entry outside a loop is inefficient.
Note 2: For your project, you should either pass the array size (10) to the functions as a parameter or use a global constant to store it. Hard-coding the literal constant 10 in function definitions that receive the array as a parameter is poor style. It should be avoided.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
