Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Combinations of a set

  1. Apr 2, 2008 #1
    I have a set {1,2,3,4,5} and I need all possible combinations with three elements. Example {1,2,3}, {1,2,4}, {1,2,5} etc.
    Can somebody help me with a 'C' program that does this. I want to store {1,2,3} etc. in an array of size three and print out every time a new combination is formed.

    Thanks,
    gradnu
     
  2. jcsd
  3. Apr 2, 2008 #2

    rcgldr

    User Avatar
    Homework Helper

    Assuming these are to be unique combinations, then an easy way to do this is 3 indexes:

    Code (Text):

        for(i = 0; i < 3; i++){
            for(j = i+1, j < 4; j++){
                (for k = j+1, k < 5; j++){
     
     
    Last edited: Apr 2, 2008
  4. Apr 3, 2008 #3
    You can do loops if they are not supposed to be unique as well.

    for (first = 1; first < 5; first++)
    for (second = 1; second < 5; second++)
    for (third = 1; third < 5; third++)
    printf("%d%d%d\n", first, second, third);

    Which should print something like "111", "112", "113", "114", "115", "121", "122", "123", ...

    k
     
  5. Apr 4, 2008 #4
    Thanks a lot guys.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Combinations of a set
Loading...