C++ Program: Lottery Application Write a program that simulates a lottery. The p
ID: 3837136 • Letter: C
Question
C++ Program: Lottery Application
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 array:
7 4 9 1 3
user array:
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 of the digits match, display a message proclaiming the user as a grand prize winner.
The program should define the following functions:
- A function generates random nunbers and stores them in the lottery array.
- A function gets the user's lottery picks.
- A function finds the number of the user's values that match the lottery numbers. The number of matches is returned.
- A function displays the values in the lottery array and the user array
//Can someone please help me with this program as well as inserting comments explaining the program? That would help a lot. Thank you!!!
Explanation / Answer
#include <iostream>
#include <stdlib.h>
using namespace std;
void generateLotteryNumber(int lottery[], int n)
{
for(int i = 0; i < n; i++)
{
lottery[i] = rand()%10;
}
}
void getUserNumbers(int user[], int n)
{
for(int i = 0; i < n; i++)
{
cout << "Enter number " << (i+1) << " : ";
cin >> user[i];
}
}
int matchCount(int lottery[], int user[], int n)
{
int matches = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(user[i] == lottery[j])
{
matches++;
break;
}
}
}
return matches;
}
void display(int arr[], int n)
{
for(int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int n = 5;
int lottery[5];
int user[5];
generateLotteryNumber(lottery, n);
getUserNumbers(user, n);
cout << "lottery array:" <<endl;
display(lottery, n);
cout << "user array:" <<endl;
display(user, n);
int matches = matchCount(lottery, user, n);
cout << "There are " << matches << " matches in lottery and users number"<< endl;
if(matches == n)
{
cout << "grand prize winner"<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.