I can generate all the possible ways to choose 3 things out of 6 things with the following code (order doesn't matter here).
1 2 3 4 5 6 7
for(int i = 0; i < 6; ++i){
for(int j = i+1; j < 6; ++j){
for(int k = j+1; k < 6; ++k){
cout << i << " " << j << " " << k << endl;
}
}
}
The problem is that I need to vary the number of items I'm choosing. Which means I have to vary the number of nested loops. Any ideas as to how I could do this? I want to know all the possible ways to choose k things out of n things, where k goes from 1 to some number less than n. I haven't had much experience writing recursive methods, but it seems like recursion might be the best way to go. Someone push me in the right direction please!
Donald Knuth has a selection of algorithms to choose from, or so I have heard.
If you want more of a push than that (a.k.a. don't want to find the book), let me know, and I will see what I can dig up. I seem to remember something from a math class I took once about something similar to this.
This solution should work, at least it did when I went through it on paper. It's my own invention... It complies but produces a 'bus error' (I'm working on a mac). I'm guessing it's because I'm not using pointers the right way.
Basically, I made a class called 'For_Loop' which is supposed to behave like it sounds. A For_Loop object is made for each item that is being chosen. The For_Loops are then connected/nested with their pointer members.