Let's say you want to print combinations of a,b,c,d,e which are {a,b,c} {a,b,d} {a,b,e} {a,c,d} {a,c,e} {a,d,e} {b,c,d} {b,c,e} {b,d,e} {c,d,e}
Let's generalize it a little, for an N,3 combination. For a combination, the order of elements in a term is not important ({a,b,c} is the same as {c,a,b}), but two elements cannot be repeated. So for simplicity, it is easier to arrange them in alphabetical order
You need to do the following steps:
1. initialize an array char letters[N] with letters[0]='a' and so on
2. loop over the first element of your output. Your index will go from 0 to N-3. Why N-3? because if I would have N-2 the second element must be letters[N-1] (alphabetically increasing) and the third would be letters[N] - out of bounds
3. loop over the second element of your output. Using the same reasoning, you start from the index of the first element + 1, and go up to N-2
4. loop over the third element of your output. This will start from the index of the second element, and will end at N-1
5. Print the combination
1 2 3 4
|
for(int i=0; i<N-3; i++)
for(int j=i+1; j<N-2; j++)
for(int k=j+1; k<N-1; k++)
cout<<"{"<<letters[i]<<","<<letters[j]<<","<<letters[k]<<"}\n";
|
Obviously printing N,p combinations in general would be a little more complicated, but still doable. See
http://stackoverflow.com/questions/12991758/creating-all-possible-k-combinations-of-n-items-in-c