IN C++ LANGUAGE. Write a program that simulates a lottery ticket consisting of 6
ID: 3775805 • Letter: I
Question
IN C++ LANGUAGE. Write a program that simulates a lottery ticket consisting of 6 single digit numbers.
The program should have a function with one array of integers (to represent a lottery ticket) as one parameter and an integer as the size of the array. The function should generate a random number in the range of 0 through 9 for each element in the array.
void generateTicket(int lottery[], int size);
The program should have another function with one array of integers (representing a biased lottery ticket) as one parameter and an integer as the size of the array. The function should generate a random number in the range of 0 through 3 for each of the first 2 elements in the array, random number in the range of 4 through 6 for each of the third and fourth elements in the array, a random number between 0 and 9 for the remaining elements in the array.
void generateBiasedTicket(int biasedLottery[], int size);
The program should have another function with one array of integers (representing a user’s lottery ticket) as one parameter and an integer as the size of the array. The function should ask the user to enter 6 numbers (each number in the range of 0 through 9) to represent the user’s ticket number. The user’s ticket should be stored in the array UserTicket.
void getUserTicket(int UserTicket[], int size);
Finally, write a function that compares the user’s ticket (UserTicket) against the ticket generated by the computer (lottery). The function should display matched and unmatched numbers between the two tickets.
void CompareTickets(int UserTicket[], int lottery[], int size);
//hint: you might need to declare an array inside this function to
//keep track of matched and unmatched digits between the two tickets.
Sample output of this function:
User Ticket : 1 5 4 0 9 6
Lottery : 1 7 0 0 9 5
Matched : Y N N Y Y N
# of matched digits = 3 at positions 1, 4, and 5.
# unmatched digits = 2 at positions 2, 3, and 6.
Complete the main function below as instructed by the comments. You need to call the functions that you created.
int main()
{
int Randomlottery[5], BiasedLottery[5], UserTicket[5];
char choice;
cout<< "Do you want to generate lottery Tickets ? Enter Y or N ";
cin>>choice;
while(choice == 'Y ' || choice == 'y ')
{
//generate random lottery ticket
//generate biased lottery ticket
//allow the user to enter ticket.
//compare user’s ticket and random lottery ticket
//compare user’s ticket and biased lottery ticket
cout<< "Do you want to generate a lottery Ticket ? ";
cin>>choice;
}
system("pause ");
return 0;
}
Explanation / Answer
Here is the code for you:
#include <iostream>
#include <cstdlib>
using namespace std;
void generateTicket(int lottery[], int size);
void generateBiasedTicket(int biasedLottery[], int size);
void getUserTicket(int UserTicket[], int size);
void CompareTickets(int UserTicket[], int lottery[], int size);
int main()
{
int Randomlottery[5], BiasedLottery[5], UserTicket[5];
char choice;
cout<< "Do you want to generate lottery Tickets ? Enter Y or N ";
cin>>choice;
while(choice == 'Y' || choice == 'y')
{
//generate random lottery ticket
generateTicket(Randomlottery, 5);
//generate biased lottery ticket
generateBiasedTicket(BiasedLottery, 5);
//allow the user to enter ticket.
getUserTicket(UserTicket, 5);
//compare user’s ticket and random lottery ticket
CompareTickets(UserTicket, Randomlottery, 5);
//compare user’s ticket and biased lottery ticket
CompareTickets(UserTicket, BiasedLottery, 5);
cout<< "Do you want to generate a lottery Ticket ? ";
cin>>choice;
}
system("pause ");
return 0;
}
//The program should have a function with one array of integers (to represent a lottery ticket)
//as one parameter and an integer as the size of the array. The function should generate a
//random number in the range of 0 through 9 for each element in the array.
void generateTicket(int lottery[], int size)
{
for(int i = 0; i < size; i++)
lottery[i] = rand() % 10;
}
//The program should have another function with one array of integers (representing a biased
//lottery ticket) as one parameter and an integer as the size of the array. The function
//should generate a random number in the range of 0 through 3 for each of the first 2 elements
//in the array, random number in the range of 4 through 6 for each of the third and fourth
//elements in the array, a random number between 0 and 9 for the remaining elements in the array.
void generateBiasedTicket(int biasedLottery[], int size)
{
for(int i = 0; i < 2; i++)
biasedLottery[i] = rand() % 4;
for(int i = 2; i < 4; i++)
biasedLottery[i] = rand() % 3 + 4;
for(int i = 4; i < size; i++)
biasedLottery[i] = rand() % 10;
}
//The program should have another function with one array of integers (representing a user’s
//lottery ticket) as one parameter and an integer as the size of the array. The function
//should ask the user to enter 6 numbers (each number in the range of 0 through 9) to represent
//the user’s ticket number. The user’s ticket should be stored in the array UserTicket.
void getUserTicket(int UserTicket[], int size)
{
cout<<"Enter "<<size<<" numbers: ";
for(int i = 0; i < size; i++)
cin>>UserTicket[i];
}
//Finally, write a function that compares the user’s ticket (UserTicket) against the ticket
//generated by the computer (lottery). The function should display matched and
//unmatched numbers between the two tickets.
void CompareTickets(int UserTicket[], int lottery[], int size)
//hint: you might need to declare an array inside this function to
//keep track of matched and unmatched digits between the two tickets.
{
cout<<"User Ticket : ";
for(int i = 0; i < size; i++)
cout<<UserTicket[i]<<" ";
cout<<endl;
cout<<"Lottery : ";
for(int i = 0; i < size; i++)
cout<<lottery[i]<<" ";
cout<<endl;
int matched = 0, unMatched = 0;
cout<<"Matched : ";
for(int i = 0; i < size; i++)
if(UserTicket[i] == lottery[i])
{
cout<<"Y ";
matched++;
}
else
{
cout<<"N ";
unMatched++;
}
cout<<endl;
cout<<"# of matched digits = "<<matched<<" at positions";
for(int i = 0; i < size; i++)
if(UserTicket[i] == lottery[i])
cout<<i+1<<" ";
cout<<endl;
cout<<"# of unmatched digits = "<<unMatched<<" at positions";
for(int i = 0; i < size; i++)
if(UserTicket[i] != lottery[i])
cout<<i+1<<" ";
cout<<endl;
}