Write a program that simulates a lottery. The program should havean array of fiv
ID: 3624860 • 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.
Explanation / Answer
You have two options because you question was a little unclear. The first runs and gives the lottery array any random number. The second gives only random numbers that have not been selected.
The first
--------------------------------------------------------------------
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <time.h>
void main ()
{
time_t Seed;
time (&Seed);
// Makes the random number based on a time seed
srand ((unsigned) Seed);
int Lottery [5];
int UserNums [5];
int NumMatch = 0;
for (int i = 0; i < 5; i++)
{
Lottery [i] = (rand () % 10);
cout << "Enter number " << (i + 1) << ": ";
cin >> UserNums [i];
cout << endl;
}
for (int i = 0; i < 5; i++)
{
if (UserNums [i] == Lottery [i])
{
NumMatch++;
}
}
cout << "Lottery numbers are: " << Lottery [0] << "," << Lottery [1] << "," << Lottery [2] << "," << Lottery [3] << "," << Lottery [4] << "," << endl;
if (NumMatch == 5)
{
cout << "Congratulations you got " << NumMatch << " matching numbers you are the grand prize winner." << endl;
}
else
{
cout << "You got " << NumMatch << " matching numbers." << endl;
}
}
----------------------------------------------------------------------------------------------
Or if lottery numbers can't repeat
----------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <time.h>
void main ()
{
time_t Seed;
time (&Seed);
// Makes the randomn number based on time
srand ((unsigned) Seed);
int Lottery [5];
int UserNums [5];
int NumMatch = 0;
bool IsSame = false;
for (int i = 0; i < 5; i++)
{
Lottery [i] = (rand () % 10);
// Loop to insure there are no matching numbers being randomnly selected
for (int k = 0; k < 5; k++)
{
if ((Lottery [i] == Lottery [k]) && (i != k))
IsSame = true;
}
if (IsSame)
{
IsSame = false;
i--;
// Jump to end of loop and go back up
continue;
}
else;
cout << "Enter number " << (i + 1) << ": ";
cin >> UserNums [i];
cout << endl;
}
for (int i = 0; i < 5; i++)
{
if (UserNums [i] == Lottery [i])
{
NumMatch++;
}
}
cout << "Lottery numbers are: " << Lottery [0] << "," << Lottery [1] << "," << Lottery [2] << "," << Lottery [3] << "," << Lottery [4] << "," << endl;
if (NumMatch == 5)
{
cout << "Congratulations you got " << NumMatch << " matching numbers you are the grand prize winner." << endl;
}
else
{
cout << "You got " << NumMatch << " matching numbers." << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.