Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Combinations (http://www.go4expert.com/forums/combinations-t1141/)

vlado_036 9Aug2006 00:53

Combinations
 
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.

shabbir 9Aug2006 08:20

Re: Combinations
 
Refer to All possible combinations of a string article.

vlado_036 10Aug2006 23:13

Re: Combinations
 
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.

shabbir 11Aug2006 14:13

Re: Combinations
 
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.

vlado_036 12Aug2006 15:06

Re: Combinations
 
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.

shabbir 12Aug2006 17:00

Re: Combinations
 
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.

http://www.go4expert.com/showthread.php?p=3161

vlado_036 13Aug2006 20:51

Re: Combinations
 
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.

shabbir 14Aug2006 06:52

Re: Combinations
 
Just edit the following.
change to the required word.
Code: C

char str[]="G4EF"; // sample small word e.g. ABCDEF
 

Change to the proper value
Code: C

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: C

for(int i=1;i<=k[0];i++) // change k[0] to (n-r) i.r. 4 in this case
 

Simple.

vlado_036 16Aug2006 18:26

Re: Combinations
 
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.

shabbir 16Aug2006 19:34

Re: Combinations
 
Quote:

ABCD ABCD BCDE BCDE CDEF CDFE DEFA DEFA
EFAB EFAB FABC FABC ABCD ABCD
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.


All times are GMT +5.5. The time now is 12:32.