#include<stdio.h>
void increment(int a[],int i) //increments the bitvector by 1
{
if(a[i]==0||i==0)
{
a[i]=1;
return;
}
else
{
a[i]=0;
increment(a,i-1);
}
}
void print_subsets(int numbers[],int bitvector[],int n) //prints all the subsets of a set
{
int i,j,count=1;
for(i=1;i<=n;i++)count=count*2; //find no of subsets
printf("No of subsets=%d\n",count);
for(i=0;i<n;i++)bitvector[i]=0; //initialise the bit vector
printf("{ }\n"); // null set is always present
for(i=1;i<count;i++)
{
increment(bitvector,n-1);
printf("{ ");
for(j=0;j<n;j++) //scan the bit vector and print the corresponding number if its bit is set
{
if(bitvector[j]==1)printf("%d ",numbers[j]);
}
printf("}\n");
}
}
main()
{
int n,i,a[10],bitvector[10];
printf("Enter no of elements:\n");
scanf("%d",&n);
printf("Enter the elements:\n");
for(i=0;i<n;i++)scanf("%d",&a[i]);
print_subsets(a,bitvector,n);
}
No comments:
Post a Comment