In a C++ Program, I need to create a lottery program. Your state is in a process
ID: 3769001 • Letter: I
Question
In a C++ Program, I need to create a lottery program.
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.
This is what I have so far, but I can't figure out how to arrange it so that it doesn't produce the winning options first.
//header files
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
//declare function prototypes
const int LOTTERY_NUMBERS = 5;
void sortRandomLottoNumbers(int list[], int listLength);
//initiate main function
int main()
{
//declare variables
int lottoNum[LOTTERY_NUMBERS];
int playerNum[LOTTERY_NUMBERS];
int i;
char decide;
int counter;
//Output for user input on playing the lottery game
cout << "WEEKLY LOTTERY GAME " << endl << endl;
cout << "Do you want to play a 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);
//get user input for choosing 5 numbers between 1 and 40.
cout << "Choose 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 << "Sorry 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;
}
Explanation / Answer
This below script is working fine without having any issues like wining options but I just did commeneted out
the displaying of generated lottery numbers before and after sorting..so that it will not show the winning
options ..see the below code where commented is in below..
It is inside main function..
//for (i = 0; i<LOTTERY_NUMBERS; i++)
// cout << lottoNum[i] << " ";
It is inside sort function..
//cout << "After sorting lottery numbers are:" << endl;
//for (index = 0; index<listLength; index++)
// cout << list[index] << " ";
// cout << endl << endl;
now pasted the updated code here seen below..
//header files
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
//declare function prototypes
const int LOTTERY_NUMBERS = 5;
void sortRandomLottoNumbers(int list[], int listLength);
//initiate main function
int main()
{
//declare variables
int lottoNum[LOTTERY_NUMBERS];
int playerNum[LOTTERY_NUMBERS];
int i;
char decide;
int counter;
//Output for user input on playing the lottery game
cout << "WEEKLY LOTTERY GAME " << endl << endl;
cout << "Do you want to play a 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);
//get user input for choosing 5 numbers between 1 and 40.
cout << "Choose 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 << "Sorry 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;
}
Output is:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.