The lottery commission wants you to write code in C++ to generate a quick pick l
ID: 3555022 • Letter: T
Question
The lottery commission wants you to write code in C++ to generate a quick pick lottery ticket. The output should be by standard out (cout) and look something like this...
This program should prompt the user for the number of draws from 1 to 5 using a data validation loop. If the user enters a zero, the program should acknowledge that and terminate, otherwise the program should display 5 blank lines before displaying the lottery ticket with the given number of draws.
Draw - this is a row on a ticket that consists of 6 randomly selected numbers with no repeating values from 1 to 53.
Ticket - this consists of 1 to 5 draws. No ticket will have less than or more than the specified number of draws. Each ticket has a ticket number based on the seed value of the standard C++ random number generator.
There are five blank lines above the "LOTTERY TICKET" heading and three spaces to the left of it. This is important or the ticket will not print correctly on the preprinted forms.
The ticket number is an unsigned integer value used to seed the standard C++ random number generator.
Each number is displayed with cout and a set width of 5 characters, right justified.
Each draw contains non-repeating random numbers from 1 to 53, sorted in ascending order.
There are 6 numbers to a row (6 numbers to a draw).
There is a maximum of 5 rows per ticket (maximum of 5 draws per ticket).
Explanation / Answer
//var section
vector<int>Ticket(5);
char Ans;
int First;
int Second;
int Third;
vector<int>Numbers(6);
int N;
int T;
bool I;
int F;
void main()
{
srand(time(NULL));
First=0;
Second=0;
Third=0;
do{ //main do while loop
system("cls");
do{ //Do while for tickets
cout<<"How many lottery tickets would you like? Please enter a number up to 5:"<<endl;
cin>>N;
if(N<1||N>5)
cout<<"Please enter a valid number of tickets."<<endl;
}while(N<1||N>5);
for(F=0;F<N;F++) //Main for loop
{
for(Third=0;Third<6;Third++) //Create randomness
{
Numbers[Third]=((rand()%50)+1);
do{ //duplicate prevention
I=false;
for(Second=0;Second<Third;Second++)
{
if(Numbers[Second]==Numbers[First])
{
I=true;
Numbers[First]=((rand()%50)+1);
}
}
}while(I==true);
}
for(First=0;First<Numbers.size();First++)
{
for(Second=First+1;Second<Numbers.size();Second++)
{ //least to greatest
if(Ticket[First]>=Ticket[Second])
{
T=Ticket[First];
Ticket[First]=Ticket[Second];
Ticket[Second]=T;
}
}
}//ends the least to greatest
for(Third=0;Third<Numbers.size();Third++) //Displays the numbers
{
cout<<Numbers[Third]<<" ";
}
cout<<endl;
}
cout<<"Run program again (Y/N)?"<<endl;
cin>>Ans;
}while(Ans=='y'||Ans=='Y');
}//end main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.