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.
#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;
}
Comments
Leave a comment