Question

In: Computer Science

C++ programming Create an application for generating lottery tickets of random numbers. Each lottery ticket has...

C++ programming

Create an application for generating lottery tickets of random numbers. Each lottery ticket has a fixed count of numbers to be played with each number selected being within a given range. For example, when the range of numbers is 1 through 100, and 5 numbers are to be played, the lottery ticket will be composed of 5 unique values between 1 and 100. The values are selected at random.

The main function is responsible for obtaining the upper range of the numbers and the count of numbers to be played from the end user. The count of numbers must be less than the upper range. The main function will call the generateLotto function to generate the lottery ticket and then call the displayTicket function to display the resulting lottery ticket. Any number of lottery tickets may be generated.

The generateLotto function should accept two parameters. The first should be the upper range for the numbers while the second parameter represents a count of the numbers to be played. For example, lotto(51, 6) will select 6 numbers at random between 1 and 51 (inclusive.) The function should return a vector of unique integer values in sorted order.

The displayTicket function accepts a vector of integers and displays the vector.

Hints: Although the standard library defines a rand() function for generating random numbers, the function may produce duplicate numbers and hence, is not suitable for this application. To ensure that unique values are generated, create a vector that contains all possible values (1 through the given upper range) and then shuffle these values using the random_shuffle() function. This will result in a vector of numbers between 1 and the upper range in random order. From this vector, select the required number of values from the beginning of the vector, adding these values to the result vector. Lastly, use the sort() function to sort the result vector.

The random_shuffle() and sort() functions require two parameters that specify the beginning and ending elements on which to operate. To specify the beginning and ending of the vector, the begin() and end() methods of the vector class can be used. For example, when the vector allNumbers contains all the values between 1 and 51, the values within the vector can be rearranged (shuffled) using: random_shuffle(allNumbers.begin(),allNumbers.end());

Solutions

Expert Solution

Thanks for the question.


Here is the completed code for this problem.

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please rate the answer.

Thanks
===========================================================================

#include <iostream>
#include<vector>
#include <cstdlib>
#include <algorithm>

using namespace std;

//The generateLotto function should accept two parameters.
vector<int> generateLotto(int upperBound, int count){
  
   vector<int> numbers;
   for(int i=1; i<=upperBound;i++)
   {numbers.push_back(i);
   }
   // shuffles the numbers
   random_shuffle(numbers.begin(),numbers.end());
  
   // create another vector that will contian the actual lottery tickets
   vector<int>pick;
   for(int c=0; c<count;c++){
       pick.push_back(numbers.at(c));
   }
  
   // sort the count in ascending order
   sort(pick.begin(),pick.end());
   return pick;
}

//The displayTicket function accepts a vector of integers and displays the vector.
void displayTicket(vector<int> &lotteryNum){
   for(int i=0;i<lotteryNum.size();i++)
   cout<<lotteryNum.at(i)<<" ";
}

int main(){
  
   int upperRange=0; int count=1;
   do{
       cout<<"Enter upper range: ";cin>>upperRange;
       cout<<"Enter count of tickets: ";cin>>count;
       if(count>upperRange){
           cout<<"The count of numbers must be less than the upper range."<<endl
               <<"Please try again."<<endl;
       }
   }while(count>upperRange || upperRange<0 || count<0);
  
   vector<int> lotteryNum = generateLotto(upperRange,count);
   displayTicket(lotteryNum);
  
  
}


Related Solutions

Create a JAVA lottery game application.  Generate four random numbers, each between 0 and 9 (inclusive).  Allow the...
Create a JAVA lottery game application.  Generate four random numbers, each between 0 and 9 (inclusive).  Allow the user to guess four numbers.  Compare each of the user’s guesses to the four random numbers and display a message that includes the user’s guess, the randomly determined four-digit number, and the amount of points the user has won as follows: No matches 0 points Any one digit matching 5 points Any two digits matching 100 points Any three digits matching 2,000 points All four...
In a lottery, each ticket has 5 one-digit numbers 0-9 on it. (with no digit repeating...
In a lottery, each ticket has 5 one-digit numbers 0-9 on it. (with no digit repeating twice) You win only if your ticket has the digits in the required order. What are your chances of winning?
*Write in C* Write a program that generates a random Powerball lottery ticket number . A...
*Write in C* Write a program that generates a random Powerball lottery ticket number . A powerball ticket number consists of 5 integer numbers ( between 1-69) and One power play number( between 1-25).
8.2: Lottery Winners A lottery ticket buyer purchases 10 tickets a week, always playing the same...
8.2: Lottery Winners A lottery ticket buyer purchases 10 tickets a week, always playing the same 10 5-digit “lucky” combinations. Write a program that initializes an array or a vector with these numbers and then lets the player enter this week’s winning 5-digit number. The program should perform 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. Here are the numbers: 13579 26791 26792 33445...
A lottery ticket is given. For this lottery, five integer numbers are chosen from 1 to...
A lottery ticket is given. For this lottery, five integer numbers are chosen from 1 to 50, and the order doesn’t matter. You win the main prize of one million dollars if your ticket matches all five numbers drawn; you win a second prize of one thousand dollars if your ticket matches exactly four of the five numbers drawn; and otherwise you win nothing. a. What’s the probability that you win the main prize? b. What’s the probability that you...
To play the PowerBall lottery, you buy a ticket that has five unique numbers in the...
To play the PowerBall lottery, you buy a ticket that has five unique numbers in the range of 1–69, and a “PowerBall” number in the range of 1–26. (You can pick the numbers yourself, or you can let the ticket machine randomly pick them for you.) Then, on a specified date, a winning set of numbers is randomly selected by a machine. If your first five numbers match the first five winning numbers in any order, and your PowerBall number...
To play the PowerBall lottery, you buy a ticket that has five unique numbers in the...
To play the PowerBall lottery, you buy a ticket that has five unique numbers in the range of 1–69, and a “PowerBall” number in the range of 1–26. (You can pick the numbers yourself, or you can let the ticket machine randomly pick them for you.) Then, on a specified date, a winning set of numbers is randomly selected by a machine. If your first five numbers match the first five winning numbers in any order, and your PowerBall number...
To play the PowerBall lottery, you buy a ticket that has five unique numbers in the...
To play the PowerBall lottery, you buy a ticket that has five unique numbers in the range of 1–69, and a “PowerBall” number in the range of 1–26. (You can pick the numbers yourself, or you can let the ticket machine randomly pick them for you.) Then, on a specified date, a winning set of numbers is randomly selected by a machine. If your first five numbers match the first five winning numbers in any order, and your PowerBall number...
C# Programming; Create a Windows Form Application that displays a scrollable list of 10 random integers...
C# Programming; Create a Windows Form Application that displays a scrollable list of 10 random integers in the range of 1 to 100. The form should also have (1) an Add button (and input textbox) for the user to add a new number to the list, (2) a Delete button to delete the current selected integer, (3) a Sort button to sort the list, (4) a Reverse button to display the list in reverse order, and (5) Display Multiple (and...
32. Every day, Jorge buys a lottery ticket. Each ticket has a probability of 0.3 of...
32. Every day, Jorge buys a lottery ticket. Each ticket has a probability of 0.3 of winning a prize. After seven days, what is the probability that Jorge has won at least one prize? Round your answer to four decimal places. 26. Charles has seven songs on a playlist. Each song is by a different artist. The artists are Shania Twain, Nick Carter, Aaron Carter, Joey McIntyre, Phil Collins, Shakira, and Michael Jackson. He programs his player to play the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT