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.

Recommended Answers

All 5 Replies

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.

#include<iostream>

#define SIZE	3

using namespace std;

void show_subset(bool b[], char c[]);
void rec_iterate(bool b[],int n, char c[]);

int main()
{
	char c[]={'c','d','e'};
	bool b[SIZE]={0};
	rec_iterate(b,0,c);
		
	return 0;
}
	
void show_subset(bool b[], char c[])
{
	bool empty = true;
	for(int i = 0; i < SIZE; ++i)
		if(b[i] == 1)
		{
			empty = 0; 
			cout<<c[i];
		}		
	if(empty)
		cout<<"empty";
	cout<<"\n";
	cin.get();
}

void rec_iterate(bool b[],int n, char c[])
{
	for(int i = 0; i <= 1; ++i)
	{
		b[n] = i;
		if(n < SIZE -1)
			rec_iterate(b, n + 1,c);
		else
			show_subset(b, c);
	}
}

output:

empty
e
d
de
c
ce
cd
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 :-|

>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.

>>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.

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.

i would need a program of qbasic to calculate the power power set of a set

commented: thank you very helpful +0
commented: I get it -- you searched for "power set," found a thread that looked relevant, and didn't stop to notice that it was six years old and in a group for a different language. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.