# Thread: Combinations with elements from different arrays

1. ## Combinations with elements from different arrays

Hello,

If someone is kind enough to help me out, I would need some help regarding this program that I'm trying to write where I would need to calculate every combination between elements from different arrays. I explain myself a little bit better: I have n arrays, I want to know all the combinations that I can create by picking 1 element from each array, so every combination of n elements (a new array), where each one is picked from a different array. The arrays don't have the same number of elements necessarily.

Thank you so much in advance to anyone willing to help and if I did not explain myself clearly please feel free to ask any question. Just dumping your assignment on us with zero effort isn't good. 3. Hello Salem. This is not an assignment, I often need to calculate these combinations so a program like this one would be very handy. Unluckily I don't have enough programming knowledge. Also, I kindly asked if anyone is willing to help me out, so if you feel dumped anything on you feel free to not help. Thank you. 4. Originally Posted by DanieleA93 Hello,

If someone is kind enough to help me out, I would need some help regarding this program that I'm trying to write where I would need to calculate every combination between elements from different arrays. I explain myself a little bit better: I have n arrays, I want to know all the combinations that I can create by picking 1 element from each array, so every combination of n elements (a new array), where each one is picked from a different array. The arrays don't have the same number of elements necessarily.

Thank you so much in advance to anyone willing to help and if I did not explain myself clearly please feel free to ask any question.

An array can be shuffled in N factorial combinations. If you are selecting M elements from a set, the number of selections you can make is N!/((M!) (N-M)!).

If you are picking one element each from a list of sets, the
number of combinations is N1 * N2 * N3 ... for the number of sets you have.

The upshot is that programs that try out every permutation of an array, or every possible selection from a set, are totally unfeasible unless the numbers are very small. However fast a processor you have. 5. Thanks a lot for your answer. With the formula that you suggested (N1 * N2 * N3 ... ) I calculated that the combinations I'm needing in this particular case should be 480 (given that I got it correctly, the arrays contain 12, 5, 2, 2 and 2 elements) of 5 elements each. Do you reckon that it's too much? Sorry for taking advantage of your kindness and thank you again. 6. Is there any reason you picked C as the Language to do this project?

Tim S. 7. Hi Tim S., I picked C since it's the only language I have a grasp on. 8. Originally Posted by DanieleA93 Thanks a lot for your answer. With the formula that you suggested (N1 * N2 * N3 ... ) I calculated that the combinations I'm needing in this particular case should be 480 (given that I got it correctly, the arrays contain 12, 5, 2, 2 and 2 elements) of 5 elements each. Do you reckon that it's too much? Sorry for taking advantage of your kindness and thank you again.
It depends if you have to handle each case with custom code or not. If all cases can be handled with the same code, then 480 is nothing. If you have to write separate functions for all 480 cases, it's maybe doable but it's a massive undertaking.

The other question to ask is, does the program have to scale? Will the arrays always be that size, or might they get bigger? As you add a few elements to the arrays, the number of combinations will grow very quickly. 9. I don't exactly understand what you mean with "separate functions for all 480 cases". I would just need to know what these 480 arrays are, i.e. the combinations, and do no further operations to them. I hope that answers the question.

Regarding the second question, the arrays might get bigger/smaller, and also increase or decrease in number (in this particular case I have 5 arrays of 12, 5, 2, 2 and 2 elements but I might have 6 of 4, 5, 2, 2, 2 and 2) but this is pretty much the "extreme case", I don't expect to ever have more than this many combinations as result. 10. So, it is time for you to start posting some code.

I would for the small number of arrays and small number of elements, use defines for the max number elements and number of arrays.

I would use one array just to track the number of elements in each array.

The data can either be in N different arrays or use an two dimension array for storing all the data.

Tim S. Popular pages Recent additions array, arrays, combinations, elements, explain 