1. ## Lottery application assignment

Hi everyone, I am having a problem to write a program that simulates a lottery. I hope someone can guide me to fix the problem/error step by step. Thank you.

Here is the question: The program should have an array of five integers named lottery, and should generate a random number in the range of 0 to 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 are 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.

Your program must use the following functions:

void generateNumbers(int [ ], int);

void getUser(int [ ], int);

int findMatches(int [ ], int [ ], int);

void displayValues(int [ ], int [ ], int);

And here is my code:
Code:
```.
// Chapter 7
// Assignment 7-Lottery Application.
#include <iostream>
#include <time.h>
using namespace std;

// Function prototype
void generateNumbers(int[], int);
void getUser(int[], int);
int findMatches(int[], int[], int);
void displayValues(int[], int[], int);

int main()
{
const int SIZE = 5;			// Size of an array.
int lotteryArray[SIZE];		// To hold the lottery's number in randomly.
int userArray[SIZE];		// To hold the user's number.

// Function begin
cout << "This program can helps you win a lottery game." << endl;

// Get the number from lottery array.
generateNumbers(lotteryArray, SIZE);

// Get the number from user.
getUser(userArray, SIZE);

// Find and match the lottery and user array.
findMatches(lotteryArray, userArray, SIZE);

// Display a message.
displayValues(lotteryArray, userArray, SIZE);

system ("PAUSE");
return 0;
}

//*************************************************
// The message displays a generateNumbe function. *
// ************************************************

void generateNumbers(int lotteryArray[], int SIZE)
{
srand(time(0));
int randNum = rand() % 9+0;	// Each elements in the array that genertate a randNum in the range of 0 - 9.

randNum = lotteryArray[0];

cout << "The lottery program will generate some random numbers "
<< "in the range of 0 to 9";
cout << "Here is " << SIZE << "element in the array";
cin >> randNum;

return 0;
}

//**********************************************
// This message displays a getUser function.   *
//**********************************************

void getUser(int userArray[], int SIZE)
{
// Loop counter
int index;

for(index = 0; index < SIZE; index++)
{
cout << "Enter " << SIZE << "digits number to win the lottery: ";
cin >> userArray[index];
}
}

//**************************************************
// This message displays a findmatches function.   *
//**************************************************

int findmatches(int lotteryArray[], int userArray[], int SIZE)
{
bool arraysEqual = true;	// Flag variable.
int count = 0;				// Loop counter varabile.

// Determine whether the elements contain the same data.
while (arraysEqual && count < SIZE)
{
if (lotteryArray[count] != userArray[count])
arraysEqual = false;
count++;
}
if (arraysEqual)
cout << "The arrays are match.\n";
else
cout << "The arrays are not match.\n";

return 0;
}

//**************************************************
// This message displays a displayValues function. *
//**************************************************

void displayValues(int lotteryArray[] , int userArray[], int SIZE)
{
int arraysEqual;

if(arraysEqual = false)
cout << "Sorry your numbers weren't match to the wining number, please try that again.\n";
else
cout << "Congratulations, you are a grand prize winner.\n";

}```
.

Here are the problems that I got:
2010\projects\improve\improve\improve.cpp(46): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
2010\projects\improve\improve\improve.cpp(57): error C2562: 'generateNumbers' : 'void' function returning a value
2010\projects\improve\improve\improve.cpp(8) : see declaration of 'generateNumbers'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

2. > srand(time(0));
Try
srand((unsigned int)time(0));

> 2010\projects\improve\improve\improve.cpp(57): error C2562: 'generateNumbers' : 'void' function returning a value
Can you see it?
Code:
```void generateNumbers(int lotteryArray[], int SIZE)
{
srand(time(0));
int randNum = rand() % 9+0; // Each elements in the array that genertate a randNum in the range of 0 - 9.

randNum = lotteryArray[0];

cout << "The lottery program will generate some random numbers "
<< "in the range of 0 to 9";
cout << "Here is " << SIZE << "element in the array";
cin >> randNum;

return 0;
}```

3. There are a lot of problems in this code. First, have a read at SourceForge.net: Do not remove parameter names - cpwiki. Though you've been given prototypes to use, presumably there is no harm in adding parameter names to them.

randNum = lotteryArray[0]; in generateNumberd is probably backwards. You are assigning the 0th element of lotteryArray to randNum, which makes no sense. Besides, that element's value is undefined at this point. I suggest you go to project settings -> C/C++ -> General and changing warning levels to 4.
Also, I assume you should generate 5 random numbers, not one.
Later, what you output to screen makes no sense with what the line of code is doing. What are you supposed to do, really?

In getUser, change
for(index = 0; index < SIZE; index++)
to
for(int index = 0; index < SIZE; index++)
and remove the int index line above. Prefer declaring variables near use. You can even define them inside loops.

displayValues is broken.
Firstly you define a new arraysEqual variable whose value is undefined (you never initialize it!). Secondly, you give it type int while clearly it is a bool. And finally, you try to check if the arrays for the user and the lottery are equal by testing this. Obviously it cannot possibly say whether they are equal since it's a variable you just defined!
To make matters worse, you've confused = for == (ie assignment for comparison).

4. It might not be a rule that is required for your assignment - but your lottery numbers could all be the same or contain lots of repeats at the moment, as you have not written anything to try and stop that happening.

5. Hello everyone, thanks for all your response. This actually is a draft code, so there are loads of error need to fix.

6. can you guide me how to fix those error? I know there are some repeat numbers and moments.

7. Be specific about what you do not understand.

8. I can't even build the random number in generateNumbers function. I am confusing how to input the random number in the generateNumbers function.
Code:
```.
void generateNumbers(int lotteryArray[], int SIZE)
{
srand((unsigned int)time(0));
int randNum = rand() % 9+0; // Each elements in the array that genertate a randNum in the range of 0 - 9.

randNum = lotteryArray[5];

cout << "The lottery program will generate some random numbers "
<< "in the range of 0 to 9." << endl;
cout << "Here are " << SIZE << " numbers in the array";
cout << randNum << endl;

}```
.

For example, I want the program can build some random numbers after it compilers. Like the instruction question says, "should generate a random number in the range of 0 to 9 for each element in the array." I am wondering did I do it in the right way, or there is another way to do it. Thank you.

9. Like I already mentioned, you have assignment backwards. You assign lotteryArray[5] to randNum, no the other way around.
Your random number generation is wrong, too. The principle is right, but you should use "% 10" to generate between 0 - 9.

10. Hmh, I think I got it. I just finish the program and it runs perfectly(no error). Thanks man

11. I think, that to be sure you did the right thing, you should post your most up-to-date code.

12. Here is the up-to-date code:
Code:
```.
// Chapter 7
// Assignment 7-Lottery Application.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int ARRAY_SIZE = 5;           // SIZE of an array.
const int MAX_RANGE = 9;            // The max range between 0 to 9.

// Function prototype
void generateNumbers(int[], int);
void getUser(int[], int);
int findMatches(int[], int[], int);
void displayValues(int[], int[], int);

int main()
{
int lottery[ARRAY_SIZE];            // To hold the lottery's number in randomly.
int user[ARRAY_SIZE];               // To hold the user's number.
int numMatches;                                   // To holf the matching number.

// Function begin
cout << "This program can helps you win a lottery game." << endl;

// Get the number from lottery array.
generateNumbers(lottery, ARRAY_SIZE);

// Get the number from user.
getUser(user, ARRAY_SIZE);

// Find and match the lottery and user array.
numMatches = findMatches(lottery, user, ARRAY_SIZE);

// Display a message.
displayValues(lottery, user, ARRAY_SIZE);

cout << "Your number matched " << numMatches << " numbers.\n";
if (numMatches == 5)
{
cout << "congratulations, you are a grand prize winner.\n";
}
else
{
cout << "Sorry, be better luck next time.\n";
}

system ("PAUSE");
return 0;
}

//*************************************************
// The message displays a generateNumbe function. *
// ************************************************

void generateNumbers(int lottery[], int ARRAY_SIZE)
{
srand((unsigned int)time(0));

cout << "The lottery program will generate some random numbers "
<< "in the range of 0 to 9." << endl;
for (int index = 0; index < ARRAY_SIZE; index++)
{
lottery[index] = 0 + rand() % MAX_RANGE;
}
cout << endl;
}

//**********************************************
// This message displays a getUser function.   *
//**********************************************

void getUser(int user[], int ARRAY_SIZE)
{

for(int index = 0; index < ARRAY_SIZE; index++)
{
cout << "Enter a number " << (index + 1) << ": ";
cin >> user[index];
}
}

//**************************************************
// This message displays a findmatches function.   *
//**************************************************

int findMatches(int lottery[], int user[], int ARRAY_SIZE)
{
bool arraysEqual = true;     // Flag variable.
int count = 0;                      // Loop counter.
int numMatches = 0;

// Determine whether the elements contain the same data.
while (arraysEqual && count < ARRAY_SIZE)
{
if (lottery[count] != user[count])
arraysEqual = false;
count++;
}
if (arraysEqual)
cout << "The arrays are match.\n";
else
cout << "The arrays are not match.\n";

return numMatches;
}

//**************************************************
// This message displays a displayValues function. *
//**************************************************

void displayValues(int lottery[] , int user[], int ARRAY_SIZE)
{
for (int i = 0; i < ARRAY_SIZE; i++)
{
cout << lottery[i] << " ";
}
cout << endl;
for (int u = 0; u < ARRAY_SIZE; u++)
{
cout << user[u] << " ";
}
cout << endl;
}```
.

13. Code:
```void generateNumbers(int[], int);
void getUser(int[], int);
int findMatches(int[], int[], int);
void displayValues(int[], int[], int);```
This still isn't fixed.
SourceForge.net: Do not remove parameter names - cpwiki

You should change
srand((unsigned int)time(0));
to
srand((unsigned int)time(nullptr));
unless your compiler complains (nullptr is the new "null" for pointers in C++; 0 is the "old null").

14. Okay, I fixed it. Thanks