You are on page 1of 4

##Powerset

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define space std

// A function to print array element according to the code in the argument list.

int print(char code[], int arr[], int n)

int i;

printf("\t{");

for(i = 0; i < n; i++)

// Print if the corresponding value is 1.

if(code[i] == '1')

printf("%d,",arr[i]);

printf("},");

printf(" {");

for(i = 0; i < n; i++)

// Print if the corresponding value is 0.

// It will print the remaining elements which on union forms the superset.

if(code[i] == '0')

{
printf("%d,",arr[i]);

}printf("},\n");

void GenUnionSet(int arr[], int n)

int i, r, l;

char binary[n];

r = pow(2, n-1);

for(i = 0; i < n; i++)binary[i] = '0';

for(i = 0; i < r; i++)

print(binary, arr, n);

l=n-1;

// Incrementing the binary value with each iteration.

h:

if(binary[l] == '0')

binary[l] = '1';

else

binary[l] = '0';
l--;

goto h;

int main()

int i, n;

printf("\nEnter the number of element array have: ");

scanf("%d",&n);

int arr[n];

printf("\n");

// Take the input of the array.

for(i = 0; i < n; i++)

printf("Enter %d Element:",i+1);

scanf("%d",&arr[i]);

int c=pow(2,n);

printf("\n Number of set is %d\n ",c);


// Print the subset using binary counting method.

printf("\nThe possible subset pairs which on union generates the Powerset, are: \n");

printf("\n {");

GenUnionSet(arr, n);

printf("\n }");

getch();

You might also like