Create a project and program named YourLastNameLottery. Write a program that sim
ID: 3700960 • Letter: C
Question
Create a project and program named YourLastNameLottery. Write a program that simulates a lottery. The program should have an array of five integers named lottery, and should generate a random number in the range of 0 to 9 for each element in the array. The user should enter 5 digits which should be stored in an integer array named user. The program should validate that the digits from the user are between 0 and 9. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that match. For example, the following shows the lottery array and the user array with sample numbers stored in each. There are two matching digits (elements 2 and 4) lottery array: 4 9 user array: 4 2 The program should display the random numbers stored in the lottery array and the number of matching digits. If all of the digits match, display a message proclaiming the user as a grand prize winner. Extra Credit Portion: Create an integer vector called matching and put into it any of the user's digits that appear in the lottery array. Finally, display the contents of the matching vector. In the above example, the matching vector would be: 4Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
#define array_size 5
using namespace std;
int main()
{
srand(time(NULL)); // replace randomize();
int num,i=0,lottery[array_size],user[array_size];
for(i=0;i<5;i++)
{
num = rand()% 10; // replace random()
lottery[i]=num;
}
for(i=0;i<5;i++)
{
cout << lottery[i] << " ";
}
cout << " ";
i=0;
int user_digit;
while(i<5)
{
cout << "ENTER USER DIGIT(0 to 9):";
cin >> user_digit;
if(user_digit>=0 && user_digit<=9)
{
user[i]=user_digit;
i++;
}
else
cout << "ENTER THE DIGIT FORM 0 TO 9;" << " ";
}
int j,count_digit_match[5],index=0,match[5],position=0;
//Finding the matching elements with same index.
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(lottery[i]==user[j] && i==j)
{
count_digit_match[index]=j;
index=index+1;
}
}
}
//Finding the matching elements.
for(i=0;i<5;i++)
{
int flag=0;
for(j=0;j<5 && flag!=1;j++)
{
if(lottery[i]==user[j])
{
match[position]=lottery[i];
flag=1;
position++;
}
}
}
cout << "matching digits in lottery and user with same index :";
for(i=0;i<index;i++)
{
cout << count_digit_match[i] << " ";
}
cout << " ";
//Matching
for(i=0;i<position;i++)
{
cout << match[i] << " ";
}
cout << " ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.