# Thread: permutating and printing all possible 5 numbers r for an array of 20 or N elements

1. ## permutating and printing all possible 5 numbers r for an array of 20 or N elements

Hey guys this a program i am to write for my college professor and he has given me a list of N elements and i am to print all possible 5 combination of elements and print to a list. However, i was thinking
Code:
`of using the rand() and srand() functions but since the number space is very large so rand() % N would produce repeated`
sequences of 5 numbers so i just wanted help how how to develop this algorithm. Here is my code but i know it is not correct.

Code:
```int Permutate_Numbers(int Array[], int n)
{
for( int i = 0; i < N C 5; i++)//counts right up to NCombination     5...doubt here??
{
srand(time(NULL));
for( int j = 0; j < 5; j++)
Result[i][j] = rand() % 5;
}
return 0;
}  //but i have to make sure no number repeates on the same```
sequence list and no sequence list repeats subsequently... well i am not really sure about my algorithm or do i need to do some permutations again to be able to get all the possible 5 combinations of the 18 elements... thanks for the help...

2. Why don't you try it on paper, for say all combinations of 2 numbers from 4 numbers.

You should be able to quickly write out the answer by hand.

Once you have the answer in front of you, you can start to visualise how to get there from your numbers.

FWIW, calling srand() in a loop is a waste of time and effort. time(NULL) will return the same value for a whole second, which is an awful lot of loop iterations.

3. Just think about trying to break it down into smaller subproblems. What would you do it if your list had <= 5 elements? Right, you'd just print the list. What about 6 elements? You'd exclude a different element each time and print the resulting lists. What about 7? You'd exclude a different element each time to get all the possible 6 combinations, and then process each of those... and so on.

4. ok thanks @Salem and @joeprogrammer what ure saying is correct i did not really take time to think about it well i'll try to write the code on paper and see for small input then continue increasing and thinking

5. however looking on paper it would not consist of N Combination 5 But N permuation 5 to be able to produce all the possible 5 sequences to be generated from the 18 elements on the list or array...thanks let me now work on an algorithm...

6. Originally Posted by Nyah Check
however looking on paper it would not consist of N Combination 5 But N permuation 5 to be able to produce all the possible 5 sequences to be generated from the 18 elements on the list or array...thanks let me now work on an algorithm...
looks like a 2 step problem:
generate unqiue combinations per joe and salem
for each combination, generate the permutations