# Combinations

Discussion in 'C' started by vlado_036, Aug 8, 2006.

1. ### vlado_036New Member

Joined:
Aug 8, 2006
Messages:
12
0
Trophy Points:
0
I have a string array with 16 elements (A01,A02,A03,...,A14,A15,A16) and i would like to create a new string array which wil contais all of the combinations with 8 elements of this array. I need a code on how to complete this task.

2. ### shabbirAdministratorStaff Member

Joined:
Jul 12, 2004
Messages:
15,292
365
Trophy Points:
83
Refer to [thread=227]All possible combinations of a string[/thread] article.

3. ### vlado_036New Member

Joined:
Aug 8, 2006
Messages:
12
0
Trophy Points:
0
This code is Ok but i do not need all of combinations and permutations. I need only the combiations and I would like the new array contains combination like this :
A01A02A03A04A05A06A07A08
A01A02A03A04A05A06A07A09
A01A02A03A04A05A06A07A10
A01A02A03A04A05A06A07A11
. . . . . . . . . . . . .
. . . . . . . . . . . . .
A09A10A11A12A13A14A15A16
I hope you understand what i mean.

4. ### shabbirAdministratorStaff Member

Joined:
Jul 12, 2004
Messages:
15,292
365
Trophy Points:
83
You treat an element as A01 and not as 3 seperate characters and apply thew above logic to get the combination.

Say I identify A01 as X A02 as Y .... and then apply the above all combination to XY... and then resubstitute them when needed.

5. ### vlado_036New Member

Joined:
Aug 8, 2006
Messages:
12
0
Trophy Points:
0
My problem is not the string A01 or A02, my problem is that i need only the combinations like this:
A01A02A03A04A05A06A07A08
A01A02A03A04A05A06A07A09
. . . . . . . . . . . .. . . . . . . . . . .
. . . . . . . . . . . . .. . . . . . . . . .
A09A10A11A12A13A14A15A16

And the program create combinations like this :

A01A02A03A04A05A06A07A08
A02A01A03A04A08A06A07A09
. . . . . . . . . . . .. . . . . . . . . . .
. . . . . . . . . . . . .. . . . . . . . . .
A16A10A11A12A13A14A15A09

If i have 8 of 16 combinations the new array must have 12870 combinatiosn not more.
I hope you understand what i mean now.

6. ### shabbirAdministratorStaff Member

Joined:
Jul 12, 2004
Messages:
15,292
365
Trophy Points:
83
Yes I got your point and you can decrease the amount of looping and get your problem solved.

In that thread I have commented the complete source code and so that should help you more.

7. ### vlado_036New Member

Joined:
Aug 8, 2006
Messages:
12
0
Trophy Points:
0
I do not uderstand what should I change in this code to complete my goal.
I will try to explain better what i would like to do. I will explain problem with smaller number of elements because 16 is too large to write all combinations.
Let say I have array with 6 elements {A,B,C,D,E,F}. Now i need to find combinations 4 of 6.
There are 15 combinations of 4 letters from this set ( n!/(r!(n-r)!)=6!/(2!(6-2)!)=15 ) and they are :
A B C D
A B C E
A B C F
A B D E
A B D F
A B E F
A C D E
A C D F
A C E F
A D E F
B C D E
B C D F
B C E F
B D E F
C D E F
What should I change in you code to have this. Maybe I can not explain good becouse English is not my mother language.

8. ### shabbirAdministratorStaff Member

Joined:
Jul 12, 2004
Messages:
15,292
365
Trophy Points:
83
Just edit the following.
change to the required word.
Code:
`    char str[]="G4EF"; // sample small word e.g. ABCDEF`
Change to the proper value
Code:
`    unsigned long num = fact(key[0]); // Chnage to n!/(r!(n-r)!)=6!/(2!(6-2)!)=15-1 = 14 as we loop till <= num`
In the Display function change the for loop as follows
Code:
`    for(int i=1;i<=k[0];i++) // change k[0] to (n-r) i.r. 4 in this case`
Simple.

9. ### vlado_036New Member

Joined:
Aug 8, 2006
Messages:
12
0
Trophy Points:
0
When i apply this changes to code I still don`t have combinations as I write in the previous message.
Here are the combinations created with new code :
ABCD ABCD BCDE BCDE CDEF CDFE DEFA DEFA
EFAB EFAB FABC FABC ABCD ABCD

I will try to explayin again my problem detailed.
I need to enter two variables (not larger then 20) N and R (N must be larger then R).
for example N=6, R=4.

I create an CString array strArray with N (6) elements
strArray[0] = "A01"
. . . . . . . . . .
. . . . . . . . . .
strArray[5] = "A06"

Now I need to create all combinations with R (4) elements of this array and store them in some new array.

10. ### shabbirAdministratorStaff Member

Joined:
Jul 12, 2004
Messages:
15,292
365
Trophy Points:
83
Adjust the display function to have all the combination and pickup the distinct strings from it. I just did not test before posting the suggestions.

11. ### vlado_036New Member

Joined:
Aug 8, 2006
Messages:
12
0
Trophy Points:
0
I taste some changes in the code but I still can`t get the output as I need.

A is caracter on the first pace, B is on the second, C in on the thirdth....
The caracters should not change their places, I need the combinations of them without changing their places.

Pls help me about it, I need to complete this goal as soon as it possible

12. ### shabbirAdministratorStaff Member

Joined:
Jul 12, 2004
Messages:
15,292
365
Trophy Points:
83
13. ### vlado_036New Member

Joined:
Aug 8, 2006
Messages:
12
0
Trophy Points:
0
OK I found some usfull code.
Here is the function which works exactly what I need but only with numbers smaller then 10.
Code:
```void Combination(CString arrayN[], __int64 n_column, CString arrayR[], __int64 r_size, __int64 r_column, __int64 loop)
{
__int64 localloop = loop;
__int64 local_n_column = n_column;

if(r_column>(r_size-1))
{
CString strCombination = "";
for(int e=0; e<r_size; e++)
strCombination += arrayR[e];
return;
}

for(__int64 i=0;i<=loop;i++)
{
arrayR[r_column]=arrayN[n_column+i];
local_n_column++;
Combination(arrayN,local_n_column,arrayR,r_size,r_column+1,localloop);
localloop--;
}
}```
I enter two numbers "N" and "R"(e.g. N=9, R=7, function shows all the combinations 7 of 9), then I create array "arrayN" with N elements and "arrayR" with R elements.
When the N and R are smaller then 10 it works excelent, but for any other number larger than 10 it does not work corectly.
Can somebody tel me what should be changed in the function to work with numbers larger than 10?

Last edited by a moderator: Aug 28, 2006

Joined:
Jul 12, 2004
Messages:
15,292