Thread: Combinations with elements from different arrays

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    5

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,542
    So, do you have any code to start with?

    Just dumping your assignment on us with zero effort isn't good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2021
    Posts
    5
    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. #4
    Registered User
    Join Date
    May 2012
    Posts
    491
    Quote Originally Posted by DanieleA93 View Post
    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.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  5. #5
    Registered User
    Join Date
    Feb 2021
    Posts
    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. #6
    Registered User
    Join Date
    May 2009
    Posts
    3,977
    Is there any reason you picked C as the Language to do this project?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Feb 2021
    Posts
    5
    Hi Tim S., I picked C since it's the only language I have a grasp on.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    491
    Quote Originally Posted by DanieleA93 View Post
    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.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  9. #9
    Registered User
    Join Date
    Feb 2021
    Posts
    5
    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. #10
    Registered User
    Join Date
    May 2009
    Posts
    3,977
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 01-18-2012, 08:09 PM
  2. how to determine all combinations of array elements
    By DaleZ in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2010, 12:44 PM
  3. How to Compare elements in arrays
    By axe in forum C Programming
    Replies: 13
    Last Post: 11-16-2007, 03:04 AM
  4. arrays with elements
    By bradleyd in forum C Programming
    Replies: 5
    Last Post: 04-10-2007, 12:00 PM
  5. Obtaining all combinations of elements...
    By Mr_Miguel in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2006, 01:33 PM

Tags for this Thread