# Combination program problem

• 10-10-2007
Garland
Combination program problem
Hello, I was thinking of writing a program that writes all numbers of one combination in all orders,just for practice purpose :

(1,2,3):

1 2 3
1 1 2
1 2 3

and so on...

The program I wrote like this:

Code:

```#include <iostream> #include <vector> using namespace std ; template <class t> vector<t> assign(vector <t> array,short co,short kam) {       vector <t> copy_array=array ;       copy_array[kam]=array[co] ;       return copy_array ; } template <class t> void comb(vector <t>array,short place,short size) //parameters -the array I am working on, itīs position, and itīs length {       if (place==array.size()-1 ) //if the position is zero       for (short x=0;x<size;x++) //then it is written on the screen       cout<<array[x]<<endl ;                        for (short tocopy=0;tocopy<size;tocopy++)        comb(assign(array,tocopy,place),place+1,size) ; //the very proccess of assigning all values }                   //clear int main() {vector <int> test ; test.push_back(1) ; test.push_back(2) ; test.push_back(3) ; short int size= test.size() ;         comb(test,0,size) ;         cin.get() ;     return 0 ; }```
However, this program ends up in runtime error, would you please at least try to tell me, where is the problem?

Thanks
Garland
• 10-10-2007
dwks
I changed your for loop to this:
Code:

```      for (short tocopy=0;tocopy<size;tocopy++) {           cout << "tocopy: " << tocopy << endl               << "place: " << place << endl               << "size: " << size << endl;           comb(assign(array,tocopy,place),place+1,size) ; //the very proccess of assigning all values       }```
The output then was
Code:

```tocopy: 0 place: 0 size: 3 tocopy: 0 place: 1 size: 3 1 1 3 tocopy: 0 place: 2 size: 3 tocopy: 0 place: 3 size: 3 tocopy: 0 place: 4 size: 3 tocopy: 0 place: 5 size: 3 tocopy: 0 place: 6 size: 3 tocopy: 0 place: 7 size: 3 tocopy: 0 place: 8 size: 3 tocopy: 0 place: 9 size: 3 tocopy: 0 place: 10 size: 3 tocopy: 0 place: 11 size: 3 tocopy: 0 place: 12 size: 3 tocopy: 0 place: 13 size: 3 tocopy: 0 place: 14 size: 3 tocopy: 0 place: 15```
and so on until it finally segfaulted at place=1994. In other words, you have an infinite loop. You never check to make sure place+1 is less than size.
• 10-10-2007
Garland
A word(or two) of gratitude
Thanks a lot for even bothering to take a look at the program, I can now, thanks to you, see where I did the mistake, so thanks again and have a nice day(or two).