Create Combinations from Set {1,2,3,4,5} with 'C' Program

  • Thread starter Thread starter gradnu
  • Start date Start date
  • Tags Tags
    Combinations Set
Click For Summary

Discussion Overview

The discussion revolves around generating all possible combinations of three elements from the set {1,2,3,4,5} using a 'C' program. Participants explore different approaches to achieve this, considering both unique and non-unique combinations.

Discussion Character

  • Technical explanation, Homework-related

Main Points Raised

  • One participant requests assistance in writing a 'C' program to generate unique combinations of three elements from the set.
  • Another participant suggests using three indexes in nested loops to generate unique combinations, although the code snippet provided contains syntax errors.
  • A different approach is proposed that allows for non-unique combinations using three nested loops, which would print combinations including repetitions.
  • One participant expresses gratitude for the responses received.

Areas of Agreement / Disagreement

Participants present different methods for generating combinations, with some focusing on unique combinations while others consider non-unique combinations. There is no consensus on a single approach, and the discussion remains unresolved regarding the best method to implement.

Contextual Notes

The code snippets provided contain syntax errors and may not function as intended without corrections. The discussion does not clarify the specific requirements for uniqueness in combinations.

gradnu
Messages
21
Reaction score
0
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
 
Technology news on Phys.org
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:
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
 
Thanks a lot guys.
 

Similar threads

Replies
4
Views
3K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
3
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K