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
SUMMARY

The discussion focuses on generating all unique combinations of three elements from the set {1,2,3,4,5} using a 'C' program. The user, gradnu, requests assistance in implementing this functionality. A solution is provided that utilizes nested loops to iterate through the set, ensuring that the combinations are unique. The proposed code structure includes three nested loops to print combinations, demonstrating a straightforward approach to the problem.

PREREQUISITES
  • Understanding of C programming language syntax
  • Familiarity with nested loops in programming
  • Knowledge of array data structures in C
  • Basic concepts of combinations and permutations
NEXT STEPS
  • Explore advanced C programming techniques for generating combinations
  • Learn about recursive algorithms for combination generation in C
  • Investigate the use of the Standard Template Library (STL) in C++ for similar tasks
  • Study performance optimization techniques for combinatorial algorithms
USEFUL FOR

This discussion is beneficial for C programmers, computer science students, and anyone interested in algorithm design and combinatorial logic.

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
2K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · 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
4K