I am able to do the following program with limitations. I am not able to successfully compare the array with lottery numbers and the array with player numbers especially when a number appears more than one time in either arrays.
Please help???????
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.
#include<iostream>
#include<ctime>
#include<cstdlib>
usingnamespace std;
constint LOTTERY_NUMBERS = 5;
void sortRandomLottoNumbers(int list[], int listLength);
int main()
{
int lottoNum[LOTTERY_NUMBERS];
int playerNum[LOTTERY_NUMBERS];
int i, j;
char decide;
//bool winner;
int counter;
cout << "********** LOTTERY GAME **********" << endl << endl;
// Asks the player whether or not he or she wants to play lottery.
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;
// Used 39 instead of 40 because we are adding 1.
// We are adding 1 because a random number can also be 0,
// and we don't want to include 0 as a lottery number.
for (i=0; i<LOTTERY_NUMBERS; i++)
cout << lottoNum[i] << " ";
cout << endl << endl;
// Here lottery numbers are being outputted to use them for candidate input to
// check the validity of program for a winning candidate.
sortRandomLottoNumbers(lottoNum, LOTTERY_NUMBERS); // Sorts the lottery numbers.
cout << "Pick 5 numbers between 1 & 40: "; // Asks the user to pick lottery numbers.
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;
}