1. ## 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.

2. What is your input parameter called, and why aren't you using it?

Having two nested loops, both using the same variable (index) is very confusing.

3. Hello Salem:

The input parameter is ( int lotteryNumbers[] ), I was trying to get the validation of the generated numbers figured out. As for not using the input parameter, I am not sure how to write that, I am having trouble understanding arrays, and passing them back and forth between functions.

4. If you are having that many problems with arrays maybe you should visit the tutorial on this site.

Arrays in C and C++ - Cprogramming.com

After that you can read the section on pointers

Pointers in C and C++ - Tutorial - Cprogramming.com

This one might be hard for you to understand, but its all important for your task at hand

Passing arrays to and from functions saf - C++ Forum

If you don't get your mind wrapped around the data types you are using then you are going to have a real had time getting the programs to behave like you want them to do. There is no shame in making a simpler program first and once it works adding to it. You said you have a problem telling if a number is already in an array you could write a shorter program to just achieve that task. Once it is finished then you can add another level of functionality.

Some of the programs I've done have neared 15k lines of code which become really fun to manage coding on your own. If I didn't test each function seperately before throwing the into the big program I would have crashes every other day. Its alot easier to track an error in 10 lines of code than it is to find in 10k lines of code.

As for your code itself look back at your do while loop and the value of used. If you find a matching number in the array one time you will get lost in that loop forever.

5. counters i and j and k is a standard convention for nested loops if you dont have anything more meaningful they need to be called

6. ## validating the generated numbers

I now have the generator working,the next problem is to validate the numbers generated to insure there are no duplications. Here is the generator code that works:

Code:
```void generateLotteryNumbers(int lotteryNumbers[])
{
const int arraySize = 5;
int index = 0;

while (index < arraySize)
{
lotteryNumbers[index] = rand() % 50 + 1;
cout << lotteryNumbers[index] << endl;
index++;
}
}```

7. Well you had something approaching a workable approach in post #1

8. I found that online but did not understand what it was doing.

9. That is why it's not a good idea to copy and paste code. If you write it yourself, you will (in theory) know exactly what it is doing.

10. Hello Matticus:
At the time I thought I could do a little research and figure it out. Do you have any useful suggestions?

11. Suggestions - yes. Close the IDE/text editor, take out a pen and paper, and try to develop an algorithm by hand. When you get an idea of how to solve the problem, this will help you structure your code accordingly. This is preferable to asking "how do I do this?" in a forum each time you get stuck. I'm not trying to be rude - this is genuine advice that will help improve your programming skills.

As to suggestions for your particular question (checking for duplicates):

There are a few ways to go about this. As a beginner, it would be best if you took a "one step at a time" approach - the algorithm won't be as efficient as it could be, but it will work when done right. Here is a suggested algorithm for you to review:

Step 1: Fill the array with random numbers
Step 2: Sort the array (bubble sort is a common sorting algorithm that I find beginner-friendly)
Step 3: Check for duplicates - now that the array has been sorted, checking for duplicates is straightforward. Basically, if value[i] == value[i+1], then you have a duplicate. If you use this method, be sure to structure your loop terminating condition carefully so you don't overrun your array! (Hint: look at the 'i+1')
Step 4: If a duplicate is found, generate a new random number for that position

12. Hello Matticus:

I don't mean to be a pain but I am in a first year class and we haven't covered how to sort an array or anything else for that matter. You seem to have a great deal of knowledge on this that I haven"t been exposed to yet. Your patience would be greatly appreciated.

13. Originally Posted by les2012
Code:
`void generateLotteryNumbers(int lotteryNumbers[])`
you cannot pass arrays in this way!

you need to do one of the following:

Code:
```template<int n>
void function(int (&arrayParameter)[n])```
or

Code:
`void function(int *arrayParameter, size_t n)`

14. Originally Posted by Elkvis
you cannot pass arrays in this way!

you need to do one of the following:

Code:
```template<int n>
void function(int (&arrayParameter)[n])```
or

Code:
`void function(int *arrayParameter, size_t n)`
I agree with the sprit of this. The best alternative is generally:
Code:
`void function(LotteryNumbersClass arrayParameter);`
As another variant on your code, one could do:
Code:
```const unsigned LOTTO_SIZE = 5;

void function(int (&arrayParameter)[LOTTO_SIZE])```