# Generating lottery numbers

• December 4th, 2012, 11:24 PM
Les2012
Generating lottery numbers
Hello Everybody.
I have another problem that I do not know how to solve. Here is the problem:

Write a function named generateLotteryNumbers. The function is passed an int array of size 5. The function should generate 5 different lottery numbers in the range 1 to 50 inclusive and place the numbers in the array. The declaration is as follows:
void generateLotteryNumbers (int lotteryNumbers []);
Note that no data is passed in to the function. The array is used to return the function results. Thus the parameter is an OUT parameter. Do not display the result. Return the result.
Do not seed the random number generator inside the function. If you seed the random number generator inside the function and the function is called many times in the same second, your function will return the same results each time it is called.

I know how to generate the numbers in the specified range but I do not know how to test for duplicates. Here is the code I have so far:

Code:

```//This program will test the "generateLotteryNumbers" function #include <iostream> #include <cstdlib> #include <ctime> using namespace std; void generateLotteryNumbers (int lotteryNumbers[]); void main() {         int lotteryNumbers[5];         srand (time(0));         generateLotteryNumbers (lotteryNumbers);         cout << lotteryNumbers << endl; } //This function will generate five lottery numbers #include <iostream> using namespace std; void generateLotteryNumbers (int lotteryNumbers[]) {         const int SIZE = 5;         int randomNumbers;         int index = 0;         int number;         bool used;         for (int index = 0; index < SIZE;  index++)         {                 int number = rand()% 50 + 1;                 do                 {                         used = false;                         for(int index = 0; index < SIZE;  index++)                         {                                                                 if( number == randomNumbers[index] )                                         used = true;                                                         }                 }while(used);                 randomNumbers[index] = number;         } }```
when I try to compile this, my compiler tells me that lines 41 and 46 require an array or pointer type.
I have no idea what that means and my instructor will not tell me. He just says that there is something wrong.
• December 5th, 2012, 12:41 AM
nuzzle
Re: Generating lottery numbers
Quote:

Originally Posted by Les2012
my compiler tells me that lines 41 and 46 require an array or pointer type.

Your teacher may not tell you but the compiler did. Don't just use randomNumbers as an array, declare it as one too.
• December 5th, 2012, 01:26 AM
new_2012
Re: Generating lottery numbers
PL check declaration "int randomNumbers" ( int variable) and its use as " randomNumbers[index] " (array element)
• December 5th, 2012, 07:06 AM
GCDEF
Re: Generating lottery numbers
Two points.

const int SIZE = 5;
should be declared outside a function and used anywhere you need to know the size of the array. You declare the array in main using the number 5, then specify the loop boundary using the constant. You should be consistent.

Second, another approach to the same problem is to populate an array with the numbers 1 - 50 than call random_shuffle to randomize them. Return the first 5 elements of the shuffled array. You don't have to worry about dupes that way.
• December 5th, 2012, 08:47 AM
Les2012
Re: Generating lottery numbers
In the instructions it tells me to generate the numbers and to validate that there are no duplicates. Also, I am not allowed to use global variables. But thank you for the suggestion I will remember it for later use.
• December 5th, 2012, 09:10 AM
GCDEF
Re: Generating lottery numbers
Quote:

Originally Posted by Les2012
In the instructions it tells me to generate the numbers and to validate that there are no duplicates. Also, I am not allowed to use global variables. But thank you for the suggestion I will remember it for later use.

The way you use SIZE in your code would be regarded as bad technique. I doubt when the instructor said don't use global variables they meant don't use a const int like that. An alternative would be to say
#define SIZE 5
before you start writing functions too.

That accomplishes the same thing.

I don't think my suggestion violates the parameter of the assignment. Your function would return 5 random numbers.
• December 5th, 2012, 10:23 AM
Les2012
Re: Generating lottery numbers
Ok. I have the generator working but now I have to do a validation (required by instructor) to insure that there are no duplicate numbers. Anyone have any suggestions?
• December 5th, 2012, 11:32 AM
Alterah
Re: Generating lottery numbers
Lets say that you are given an array of 5 numbers. You have no idea what they are. But, then a user enters a number. How would you test to see if this number is in that array?
• December 5th, 2012, 01:00 PM
GCDEF
Re: Generating lottery numbers
I'd make the outer loop a while loop instead of a for loop, keep track of how many numbers you've added to the output array and keep looping till that number = 5. Generate the random number. Use your inner for loop to see if it's in the array. If it's not, add it to the output array and increment the counter.