473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Generate All Subsets

I been thinking about this topic for a long time. The best I have done
is the following code:

#include <iostream>
using namespace std;
#include <cmath>

int main(){

const int SIZE=3;
int set[SIZE]={1,2,5};

for(int i=pow(SIZE,2)-1; i>=0; --i){
int index=2;
int n=i;
cout << "{ ";
while(n>0){
if(n%2)
cout << set[index] << " ";
--index;
n/=2;
}
cout << "}" << endl;
}

system("pause");
return 0;
}

However it seems bulky to me. There is another way?

Thank you!

Nov 15 '05 #1
7 8858
In article <11**********************@g49g2000cwa.googlegroups .com>,
Gaijinco <ga******@gmail.com> wrote:
I been thinking about this topic for a long time. The best I have done
is the following code: #include <iostream>
using namespace std;
#include <cmath>
Unfortunately that is C++ code, and this is the C language newsgroup.
system("pause");
Although system() is a standard C library call, the behaviour it
invokes is system-dependant. In particular, "pause" is not a
particularily widely supported system command.
However it seems bulky to me. There is another way?


You did not actually state what problem you are trying to solve,
at least not in clear terms. You also didn't indicate what the
restrictions were -- how big of a set does the code need to be
able to handle?

You might find, by the way, that the code is a lot cleaner if
you use the binary operators.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Nov 15 '05 #2
Gaijinco wrote:
I been thinking about this topic for a long time.
Then think longer. You have posted to comp.lang.c and
The best I have done
is the following code:
"your best" starts with 3 lines which are C++. The second line is a
syntax error in C; the first and third #include non-standard headers.
#include <iostream>
using namespace std;
#include <cmath>
I have rewritten your code in C, removing the wasteful (in time and
space) call of pow(), fixing your left shifts of the undefined variable
'cout'. I have not moved the declaration of the loop control variable,
even though a syntax error before C99.

#include <stdio.h>

int main(void)
{

int set[] = { 1, 2, 5 };
const int setsize = sizeof set / sizeof *set;

for (int /* warning: declaration assumes C99 */ i =
(1 << setsize) - 1 /* removed use of pow() */ ;
i >= 0; --i) {
int index = 2;
int n = i;
putchar('{');;
while (n > 0) {
if (n % 2)
printf("%d ", set[index]);
--index;
n /= 2;
}
puts("}");
}

return 0;
}
[OP's code continues]

int main(){

const int SIZE=3;
int set[SIZE]={1,2,5};

for(int i=pow(SIZE,2)-1; i>=0; --i){
int index=2;
int n=i;
cout << "{ ";
while(n>0){
if(n%2)
cout << set[index] << " ";
--index;
n/=2;
}
cout << "}" << endl;
}

system("pause");
return 0;
}

Nov 15 '05 #3
Gaijinco wrote:
I been thinking about this topic for a long time. The best I have done
is the following code:

#include <iostream>
Not a standard C header.
using namespace std;


<snip>

Not C.

I think you walked through the wrong door, comp.lang.c++ is just down
the hall to your left. However, you should check their FAQ and a few
days posts before you post there. You can't have done that here or you
would have known that we discuss C.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #4
# However it seems bulky to me. There is another way?

Bulky is an aesthetic concern. You got the primary issue that
binary representations of 0..2^(n-1) model the power set 2^S
where |S|=n. Using machine integers restricts |S| is to a small
integer; the arithmetic is simple enough you can actually
represent the numbers with arbitrary length byte strings.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I have no idea what you just said.
I get that alot.
Nov 15 '05 #5
Thank you for your posts! I apologize but all the c++ code, I post it
here because what I thought was important was the essence of the
problem and not the syntaxis. Again: my mistakes and I apologize.
You did not actually state what problem you are trying to solve,
at least not in clear terms. You also didn't indicate what the
restrictions were -- how big of a set does the code need to be
able to handle?
The problem is that given a set you must construct (print, whatever)
all possible subsets. The main issue is not size limitations but "how"
I have rewritten your code in C, removing the wasteful (in time and
space) call of pow(), fixing your left shifts of the undefined variable
'cout'. I have not moved the declaration of the loop control variable,
even though a syntax error before C99.


OK, using (1 << setsize) -1 in exchange of pow() is pretty neat .
Thanks! But I'm in what is a C99.

Then again, the answer is still unresolved: There is another way to
construct all possible subsets?

Nov 15 '05 #6


Gaijinco wrote On 09/26/05 14:33,:
[...]
Then again, the answer is still unresolved: There is another way to
construct all possible subsets?


Of course there are other ways, but your question is
about algorithms, not about C. If you find another way
and have questions about how to implement it in C, come
back and ask. (If you have questions about how to do it
in C++, ask in comp.lang.c++ instead.)

<off-topic>

Consider a recursive solution. First, it's easy to
generate all the subsets of a one-element set: they are
the empty set and the set containing only the single
element. Next, once you have a way to generate all the
subsets of an N-element set it's easy to generate all
the subsets of an (N+1)-element set: they are all the
given subsets "as is," plus those same subsets but with
the (N+1)st element added to each.

Remember, you asked only for "another way," not for
"another good way" ...

</off-topic>

--
Er*********@sun.com

Nov 15 '05 #7
Gaijinco wrote on 26/09/05 :
#include <iostream>


c++ is next door...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon | last post by:
Hi, I'm hoping you could show me examples of how a functional/declarative language could be used to consicely describe resticted subsets of elements. I'm looking for a 'specification' style...
4
by: Brett Calcott | last post by:
I've found some python solutions to find the set of subsets for a given set, but how do you find the set of the set of subsets whose union is the given set and whose intersections is the empty set....
3
by: Xah Lee | last post by:
20050226 exercise: generate all possible pairings given a list that is a set partitioned into subsets, generate a list of all possible pairings of elements in any two subset. Example: ...
7
by: Gaijinco | last post by:
I been thinking about this topic for a long time. The best I have done is the following code: #include <iostream> using namespace std; #include <cmath> int main(){ const int SIZE=3;
1
by: MB | last post by:
I am trying to work out why .NET 1.1 SP1 (XSD.exe) cannot generate wrapper classes for LandXML 1.0 or 1.1 (http://www.landxml.org/spec.htm) and yet .NET 2.0 (BETA 1) can for LandXML 1.0. The...
25
by: Jessica Weiner | last post by:
I have an array of n integers and I want a function that returns a list of arrays of all possible subsets. Can someone provide me with the code? Thanks. Jess
4
by: LurfysMa | last post by:
I could use some help with a table design problem. I have an electronic flashcard program. Actually, several of them. They each rely on a utility program to keep track of the usage statistics....
2
by: HuifangZhang | last post by:
Could any one help with recursive functions on generating subsets? Write a function ListSubsets that generates all possible subsets of a given set, where the set is represented by a string of...
4
by: Patrick | last post by:
Hi, I want to write a programs that checks if a set of numbers in a list obey a condition, the problem is that i have say "n" numbers and i need to check all subsets of the n numbers for the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.