Answer to Question #200289 in C++ for Shob

Question #200289

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 through 9 for each element in the array. The user should enter five digits, which should be stored in an integer array named user. 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 digits: 7 4 9 1 4

User digits:  4 2 9 7 3

The program should display the random numbers stored in the lottery array and the number of matching digits. If all the digits match, display a message proclaiming the user as a grand prize winner.


1
Expert's answer
2021-05-29T03:36:54-0400
#include <iostream>

using namespace std;

int main()
{
  
srand(time(NULL));
//declaration of variables and lottery Array
int lottery[5],i=0,j=0,c=0;
//declaration of user array
int user[5];
//runs the loop
for(i=0;i<5;i++)
{
//generates the random numbre between 0-9 and stores it in lottery array
lottery[i] = rand()%((9+1)-1) + 1;
  
}
cout<<"Enter Digits: ";
//runs the loop to read digits from user
for(i=0;i<5;i++)
{
//read input from user and store it in user array
cin>>user[i];
}
cout<<"Lottery Array:\n";
//runs the loop to print lottery array
for(i=0;i<5;i++)
{
//printing the elements of lottery array
cout<<lottery[i]<<"\t";
}
//runs the loop to make comparasion of digits between lottery and user array
for(i=0;i<5;i++)
{
  
for(j=0;j<5;j++)
{
//checks the condition matching or not
if(user[i]==lottery[j]){
//c variable keep count of matching digits
c++;
}
}
}
//printing the statement count of matching digits
cout<<"\nThe Number of Matching digits: "<<c;
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog