In C++ ( New version of C++) Lottery Write a program that simulates a lottery. T
ID: 3686974 • Letter: I
Question
In C++ ( New version of C++)
Lottery
Write a program that simulates a lottery. The program should have an array of 5 integers, with a randomly generated number in the range of 0-9 for each element in the array. The program should ask the user to enter 5 digits and should store them in a second integer array. The program must compare the corresponding elements in the two arrays and count how many digits match. For example the following shows the winning digits and the players array with sample numbers stored in each. There are two matching digits, elements 2 and 4.
Winning Digits 7 4 9 1 3
Player 4 2 9 7 3
Once the user has entered a set of numbers, the program should display the winning digits and the player's digtis and tell how many digits matched.
Explanation / Answer
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
int lottery[5];
int userInput[5];
for(int i=0; i<5; i++){
lottery[i] = rand()%10; // random number 0-9
}
cout<<"Enter 5 number (0-9): "<<endl;
for(int i=0; i<5; i++){
cin>>userInput[i];
}
cout<<endl;
cout<<"Winning digits: ";
for(int i=0; i<5; i++){
cout<<lottery[i]<<" ";
}
cout<<endl;
int count = 0;
cout<<"Player digits: ";
for(int i=0; i<5; i++){
if(lottery[i] == userInput[i])
count++;
cout<<userInput[i]<<" ";
}
cout<<endl;
cout<<count<<" digits matched"<<endl;
return 0;
}
/* sample run:
Enter 5 number (0-9):
4 2 9 7 3
Winning digits: 3 6 7 5 3
Player digits: 4 2 9 7 3
1 digits matched
*/