Your state is in a process of creating a weekly lottery. Once a week, five disti
ID: 3764869 • 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. .. C++ please
Explanation / Answer
Here is the logic for you. If you need any further modifications, just get back to me.
#include <iostream>
using namespace std;
void sort(int lottery[5]) //Bubble sort.
{
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5-i-1; j++)
if(lottery[j] > lottery[j+1])
{
int temp = lottery[j];
lottery[j] = lottery[j+1];
lottery[j+1] = temp;
}
}
void automateLottery(int lottery[5]) //Generate random numbers, and sorts the list.
{
srand(time(NULL));
for(int i = 0; i < 5 ;i++)
lottery[i] = rand() % 40 + 1;
sort(lottery);
}
int main()
{
int lottery[5], guesses[5], again;
do
{
automateLottery(lottery); //Calls the function, to generate random numbers and sort.
cout<<"Select 5 distinct numbers: "; //Reading numbers from user.
for(int i = 0; i < 5; i++)
cin>>guesses[i];
sort(guesses); //Sorting user guesses.
bool won = true;
for(int i = 0; i < 5; i++) //Checking both the arrays.
if(lottery[i] != guesses[i]) //If some element doesn't match, display the lottery elements and exit the loop.
{
cout<<"You loose!"<<endl;
cout<<"The lottery elements are: ";
for(int i = 0; i < 5; i++)
cout<<lottery[i]<<" ";
cout<<endl;
won = false;
break;
}
if(won == true) //If won the game, display relavent message.
cout<<"You win."<<endl;
cout<<"Do you want to play again 1. Yes. 2. No.: "; //Play again choice.
cin>>again;
}while(again == 1);
cout<<"Thanks for playing. Bye."<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.