473,586 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8874
In article <11************ **********@g49g 2000cwa.googleg roups.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
7524
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 definition so any ideas/input would be very welcome. Thanks for your time, Simon. --
4
1936
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. ie. Given a cake divided into 6 unique pieces (0-5), how many different ways can I distribute the cake so that there are no pieces left. eg. ...
3
1982
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: genpair( ,,] ); returns:
7
1477
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
2281
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 following tests show the results of using XSD.exe in each of the four combinations: ..NET 1.1 SP1 with LandXML 1.0: Error: Error generating classes for...
25
5852
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
1678
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. After each practice session, the utility program tells the flashcard program which items were learned and whihc need more practice. The drill...
2
2937
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 letters. If you call the function: ListSubsets("ABC") your function should produce the following output: ABC AB AC A
4
3607
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 condition. How do i go about asking c++ to find the subsets and then check?? Thanks: Patrick
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8336
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.