Write a program that simulates a lottery. The program should havean array of fiv
ID: 3617109 • Letter: W
Question
Write a program that simulates a lottery. The program should havean array of five integers named lottery, and shouldgenerate a random number in the range of 0 through 9 for eachelement in the array. The user should enter five digits whichshould be stored in an integer array named user. Theprogram is to compare the corresponding elements in the two arraysand keep a count of the digits that match. For example, thefollowing shows the lottery array and the user array with samplenumbers stored in each. There are two matching digits (elements 2and 4).lottery array: 7,4,9,1,3
user array: 4,2,9,7,3
The program should display the random numbers stored in the lotteryarray and the number of matching digits. If all the digits match,display a message proclaiming the user as a grand prize winner.
NOTE: The lottery selections either by Joe User or those generatedby the computer must be unique, that is, the entered and generatednumbers can not replicate. Your code will need to make sure lotterynumbers have not occurred before as they are entered by the userand generated by the computer.
Explanation / Answer
#include<iostream> #include<stdlib> //for Randum Number function //using namespace std; int main() { int lottery[5],num[5],count=0; for(short v=0;v<=4;v++) lottery[v]=rand()%9+1; //Generating random numbers for thelottery
cout<<"Please Enter your numbers (single digit at atime) :"; for(v=0;v<=4;v++) cin>>num[5]; //getting user input cout<<" Lottery Numbers are: "; for(v=0;v<5;v++) cout<<lottery[v]<<" "; //printing lotterynumbers for(v=0;v<5;v++) { if(num[v]==lottery[v]) //checking how much numbers match count++; } if(count==5) //All number match then user win Grandprize cout<<"You win Grand Prize"; else // else a message for try once again cout<<" Try Again"; system("pause"); return 0; } #include<iostream> #include<stdlib> //for Randum Number function //using namespace std; int main() { int lottery[5],num[5],count=0; for(short v=0;v<=4;v++) lottery[v]=rand()%9+1; //Generating random numbers for thelottery
cout<<"Please Enter your numbers (single digit at atime) :"; for(v=0;v<=4;v++) cin>>num[5]; //getting user input cout<<" Lottery Numbers are: "; for(v=0;v<5;v++) cout<<lottery[v]<<" "; //printing lotterynumbers for(v=0;v<5;v++) { if(num[v]==lottery[v]) //checking how much numbers match count++; } if(count==5) //All number match then user win Grandprize cout<<"You win Grand Prize"; else // else a message for try once again cout<<" Try Again"; system("pause"); return 0; }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.