942,790 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9858
  • C++ RSS
Nov 2nd, 2004
0

Producing Power set

Expand Post »
i will be given a set of any length and my program will have to produce the power set.
For example:
input: c d e

output:
empty
c
d
e
cd
de
ce
cde

total number of subsets: 2^3 = 8

I will try to write my own code, but need to to know what the algorithm would be like( an efficient one), recursive or non-recursive whatever be ur suggestion plz do explain.
Similar Threads
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Nov 8th, 2004
0

Re: Producing Power set

Thanx for the great response

However i was working on it and prouduced this so far:
The idea is like if the binary array has elements like 0 0 1, then print the third element(corresponding entry) of the given set(char array a) which is 'c'. So if i go on like this from 0 0 0 to 1 1 1 i will get all the combinations.
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2.  
  3. #define SIZE 3
  4.  
  5. using namespace std;
  6.  
  7. void show_subset(bool b[], char c[]);
  8. void rec_iterate(bool b[],int n, char c[]);
  9.  
  10. int main()
  11. {
  12. char c[]={'c','d','e'};
  13. bool b[SIZE]={0};
  14. rec_iterate(b,0,c);
  15.  
  16. return 0;
  17. }
  18.  
  19. void show_subset(bool b[], char c[])
  20. {
  21. bool empty = true;
  22. for(int i = 0; i < SIZE; ++i)
  23. if(b[i] == 1)
  24. {
  25. empty = 0;
  26. cout<<c[i];
  27. }
  28. if(empty)
  29. cout<<"empty";
  30. cout<<"\n";
  31. cin.get();
  32. }
  33.  
  34. void rec_iterate(bool b[],int n, char c[])
  35. {
  36. for(int i = 0; i <= 1; ++i)
  37. {
  38. b[n] = i;
  39. if(n < SIZE -1)
  40. rec_iterate(b, n + 1,c);
  41. else
  42. show_subset(b, c);
  43. }
  44. }

output:
C++ Syntax (Toggle Plain Text)
  1. empty
  2. e
  3. d
  4. de
  5. c
  6. ce
  7. cd
  8. cde
But i was wondering if i could produce the output like this:
empty
c
d
e
cd
de
ce
cde

i think the way i iterated the boolian array should be altered a little. A little help would be really really appreciated :-|
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Nov 8th, 2004
0

Re: Producing Power set

>Thanx for the great response
Bite me. Now I'm not going to help you. But all is not lost because I found a C++ implementation of this on google that would give you a nice start. It took me about 7 seconds.
Administrator
Reputation Points: 6442
Solved Threads: 1391
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 8th, 2004
0

Re: Producing Power set

>>Bite me. Now I'm not going to help you.
Touchy, HUH
But i was just kiddin, man! Cant even handle a joke??? Now come on man, help me out.

>> I found a C++ implementation of this on google that would give you a nice start.
I was trying to get help with "my own devised code". If i wanted to snatch the code from a website i would have already done so instead of spending my time here and not to mention the time to write my own code. But i wanted to do it myself with the help of pros like u so that i get a better understanding.

I got a tough day coming tomorrow : Midterm and quiz on Linear Algebra, quiz on Calculus and then this programming assignment. I am really getting quite restless. I apologise if i made any offense to any one bcos of that.
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Nov 8th, 2004
0

Re: Producing Power set

I would try to solve the sub-problem of finding all character permutations in a string of a given length (l) that consists of n ones and l-n zeros. Then it's easy to just solve the problem first for n=1 and then increment n until it reaches l. Try that approach and see if you manage to solve your problem.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gallas is offline Offline
9 posts
since Nov 2004
Jan 28th, 2011
-3
Re: Producing Power set
i would need a program of qbasic to calculate the power power set of a set
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elemes is offline Offline
1 posts
since Jan 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: array of pointers
Next Thread in C++ Forum Timeline: string arrays nedd help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC