-
I need HELP!!!
Write a program that simulates a lottery. The program should have an array of five integers
named lottery, and should generate a random number in the range of 0 through 9 for each
element in the array. The user should enter five digits which should be stored in an integer
array named user. The program is to compare the corresponding elements in the two arrays
and keep a count of the digits that match. For example, the following shows the lottery array
and the user array with sample numbers stored in each. There two matching digits (elements
2 and 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 lottery array and the number of
matching digits. If all of the digits match, display a message proclaiming the user as a grand
prize winner of $25,000. If there are four matching digits, the prize should be $1,000, and if
there are three matching digits, the prize should be $500.
-
Step 1: Read the sites homework policy to understand why no one will give you a complete solution.
Step 2: Post your attempt, being sure to use code tags, and we will assist you with any specific problems you have.
Oh, and welcome to the forums.
-
1 Attachment(s)
-
Write a program that simulates a lottery. The program should have an array of five integers
named lottery, and should generate a random number in the range of 0 through 9 for each
element in the array. The user should enter five digits which should be stored in an integer
array named user. The program is to compare the corresponding elements in the two arrays
and keep a count of the digits that match. For example, the following shows the lottery array
and the user array with sample numbers stored in each. There two matching digits (elements
2 and 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 lottery array and the number of
matching digits. If all of the digits match, display a message proclaiming the user as a grand
prize winner of $25,000. If there are four matching digits, the prize should be $1,000, and if
there are three matching digits, the prize should be $500.
I made it and I have problems to make it match 4 digits and 3 also.
My work is:
Code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
int main ()
{
const int SIZE = 5; //Size of the arrays.
int user[SIZE]; //To hold the user's lucky numbers.
int lottery[SIZE]; //To hold the random numbers.
int count = 0; //Loop counter.
bool numMatch = true; //Flag variable.
cout << "Do you feel lucky today? If so, enter your " <<SIZE<<" lucky numbers \n";
cout << "between 0 and 9: \n";
//Get five numbers from the user and store them in the user array.
for (count = 0; count < SIZE; count++)
{
cout << "Please enter lucky number # " <<(count+1)<<":";
cin >> user[count];
}
//Generate five random numbers and store them in the lottery array.
srand((unsigned int)time(0));
for (count = 0; count < SIZE; count++)
{
lottery[count] = 1 + rand()%9;
}
//Display the contents of lottery array.
cout << "The winning numbers are: \n";
for (count =0; count < SIZE; count++)
{
cout <<" " <<lottery[count];
cout << endl;
}
//Determine whether there are matching numbers.
for (count =0; count < SIZE; count++)
{
while (numMatch && count < SIZE)
{
if (user[count] != lottery [count])
numMatch = false;
count++;
}
}
if (numMatch)
cout << "Congratulations you are the Grand Prize Winner!!!\n";
else if (numMatch = 4)
cout << "Congratulations you win 1,000!!!\n";
else if (numMatch = 3)
cout << "Congratulations you win 500!!!\n";
else
cout << "Sorry you did not pick the winning numbers, please try again.\n";
system("pause");
return 0;
}
-
Code:
for (count =0; count < SIZE; count++)
{
while (numMatch && count < SIZE)
{
if (user[count] != lottery [count])
numMatch = false;
count++;
}
}
if (numMatch)
cout << "Congratulations you are the Grand Prize Winner!!!\n";
else if (numMatch = 4)
cout << "Congratulations you win 1,000!!!\n";
else if (numMatch = 3)
cout << "Congratulations you win 500!!!\n";
else
cout << "Sorry you did not pick the winning numbers, please try again.\n";
First of all, the red marked code you are trying to compare a bool with a number (actually you are assigning a number but I get the gist of it). Second the green marked code does not do what you (probably) think it does.
-
Thank you, but how can I fix this? how can I make the program print 25 grand for all the numbers match, and 1,000 to 4 numbers match and 500 to 3 numbers match?
-
A while loop is unneccesary, use an inner for loop instead - make numMatch an integer (or unsigned seeing as it is never meant to be <0 ) and add one to it every time a match is found
The outer loop can start with the first lottery number then in the inner loop you compare aginst each of you user numbers, if its a match - numMatch++.
When you have compared all the lottery numbers against all of your user numbers then check if the total in numMatch is equal to that required to win a prize
Of course you need to guanrantee your user has not repeated any numbers in their selection and the same goes for the lottery numbers