# compute the subsets of a set

• 06-01-2003
k_w_s_t_a_s
compute the subsets of a set
Hi.I want to compute all the subsets of a set.For example for the set:
{1,2,3,4}
I want to find the following:
{1}
{2}
{3}
{4}
{1,2}
{1,3}
{1,4}
{2,3}
{2,4}
{3,4}
{1,2,3}
{1,2,4}
{1,3,4}
{2,3,4}
{1,2,3,4}

But I want to do it iteratively NOT recursively.
Are there any suggestion?
I'm not looking for the code.Just an algorithm or some ideas.

• 06-01-2003
WaltP
Use nested loops. Outer loop defines the number of values in the subset. Then you'll need a couple loops to iteratively get the subsets.
• 06-01-2003
k_w_s_t_a_s
The thing is that i don't know the size of the set.The size changes.The function should take the size of the set as an argument and it should succeed for every set.
• 06-01-2003
Draco
You could probably store your sets in a way that you could use strlen() on them.
• 06-01-2003
quzah
Quote:

Originally posted by k_w_s_t_a_s
The thing is that i don't know the size of the set.The size changes.The function should take the size of the set as an argument and it should succeed for every set.
You don't need to.

a) Get a set.
b) Count the members.
c) Store the count in a variable.
d) Use something like:
Code:

```for( x = 0; x < count; x++ )     for( y = x; y < count; y++ )```
That's just an illustration, not complete code naturally. But it does at least show you that it doesn't matter how long your set is here for you to get at least the first portion of your example done.

Quzah.