Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your state is in a process of creating a weekly lottery. Once a week, five disti

ID: 3767840 • Letter: Y

Question

Your state is in a process of creating a weekly lottery. Once a week, five distinct random integers between 1 to 40 (inclusive) are drawn. If a player guesses all of the numbers correctly, the player wins a certain amount.

Write a program that does the following:

a. Generates five distinct random numbers between 1 and 40 (inclusive) and stores them in an array.

b. Sorts the array containing the lottery numbers.

c. Prompts the player to select five distinct integers between 1 and 40 (inclusive) and stores the numbers in an array. The player can select the numbers in any order, and the array containing the numbers need not be sorted.

d. Determines whether the player guessed the lottery numbers correctly. If the player guessed the lottery numbers correctly, it outputs the message “You win!” otherwise it outputs the message "You lose!" and outputs the lottery numbers.

Your program should allow a player to play the game as many times as the player wants to play. Before each play, generate a new set of lottery numbers.

Answer must be in C++.

Explanation / Answer

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
const int LOTTERY_NUMBERS = 5;
void sortRandomLottoNumbers(int list[], int listLength);
int main()
{
int lottoNum[LOTTERY_NUMBERS];
int playerNum[LOTTERY_NUMBERS];
int i;
char decide;
//bool winner;
int counter;
cout << "********** LOTTERY GAME **********" << endl << endl;
cout << "Do you want to play lottery (Enter Y for Yes and N for No): ";
cin >> decide;
cout << endl;
while (decide == 'Y' || decide == 'y')
{
srand(time(0));
cout << "Generated lottery numbers: ";
for (i=0; i<LOTTERY_NUMBERS; i++)
lottoNum[i] = rand() % 39 + 1;
for (i=0; i<LOTTERY_NUMBERS; i++)
cout << lottoNum[i] << " ";
cout << endl << endl;
sortRandomLottoNumbers(lottoNum, LOTTERY_NUMBERS);
cout << "Pick 5 numbers between 1 & 40: ";   
for (i=0; i<LOTTERY_NUMBERS; i++)
cin >> playerNum[i];
cout << endl;
counter=0;
for (i=0; i<LOTTERY_NUMBERS; i++)
{
for (int j=0; j<LOTTERY_NUMBERS; j++)
if (playerNum[i] == lottoNum[j])
counter++;
}
if (counter==5)
cout << "You Win !" << endl << endl;
else
{
cout << "You Lose !" << endl;
cout << "The winning numbers are: ";
for (i=0; i<LOTTERY_NUMBERS; i++)
cout << lottoNum[i] << " ";
cout << endl << endl;
}
cout << "Do you want to play lottery (Enter Y for Yes and N for No): ";
cin >> decide;
cout << endl;
}
return 0;
}
void sortRandomLottoNumbers(int list[], int listLength)
{
int index;
int temp;
int location;
int smallIndex;
for (index=0; index<listLength-1; index++)
{
smallIndex = index;
for (location=index+1; location<listLength; location++)
if (list[location] < list[smallIndex])
smallIndex = location;
temp = list[smallIndex];
list[smallIndex] = list[index];
list[index] = temp;
}
cout << "After sorting lottery numbers are:" << endl;
for (index=0; index<listLength; index++)
cout << list[index] << " ";
cout << endl << endl;
}