Combinations of a set

  • Thread starter gradnu
  • Start date
  • #1
21
0

Main Question or Discussion Point

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
 

Answers and Replies

  • #2
rcgldr
Homework Helper
8,696
528
Assuming these are to be unique combinations, then an easy way to do this is 3 indexes:

Code:
    for(i = 0; i < 3; i++){
        for(j = i+1, j < 4; j++){
            (for k = j+1, k < 5; j++){
 
Last edited:
  • #3
238
0
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
 
  • #4
21
0
Thanks a lot guys.
 

Related Threads on Combinations of a set

  • Last Post
Replies
7
Views
3K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
1
Views
2K
Replies
3
Views
2K
Replies
4
Views
9K
Replies
9
Views
252
Replies
3
Views
1K
Replies
4
Views
618
  • Last Post
Replies
4
Views
10K
Top