|
Power Set
This example shows generating power set for the user given set.
A powset is the one which contains all possible subsets of given set.
04 | void print_set( int size, int *array, int n) { |
11 | for (i=0; i < size; i++) { |
17 | printf ( "%d" , array[i]); |
28 | void print_powerset( int size, int *array) { |
30 | int max = pow (2, size); |
34 | for (i=0; i < max; i++) { |
39 | print_set(size, array, i); |
45 | int main( int argc, char *argv[]) { |
47 | printf ( "Enter the size of the integer set: " ); |
50 | array=( int *) malloc ( sizeof ( int )); |
52 | printf ( "\nEnter the elements of the set : " ); |
55 | scanf ( "%d" , &array[i]); |
58 | print_powerset(n, array); |
|
It gives the following output,
Enter the size of the integer set: 6
Enter the elements of the set : 1 2 3 4 5 6
{ {}, {1}, {2}, {1, 2}, {3}, {1, 3}, {2, 3}, {1, 2, 3}, {4}, {1, 4}, {2, 4}, {1,
2, 4}, {3, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}, {5}, {1, 5}, {2, 5}, {1, 2,
5}, {3, 5}, {1, 3, 5}, {2, 3, 5}, {1, 2, 3, 5}, {4, 5}, {1, 4, 5}, {2, 4, 5}, {1
, 2, 4, 5}, {3, 4, 5}, {1, 3, 4, 5}, {2, 3, 4, 5}, {1, 2, 3, 4, 5}, {6}, {1, 6},
{2, 6}, {1, 2, 6}, {3, 6}, {1, 3, 6}, {2, 3, 6}, {1, 2, 3, 6}, {4, 6}, {1, 4, 6
}, {2, 4, 6}, {1, 2, 4, 6}, {3, 4, 6}, {1, 3, 4, 6}, {2, 3, 4, 6}, {1, 2, 3, 4,
6}, {5, 6}, {1, 5, 6}, {2, 5, 6}, {1, 2, 5, 6}, {3, 5, 6}, {1, 3, 5, 6}, {2, 3,
5, 6}, {1, 2, 3, 5, 6}, {4, 5, 6}, {1, 4, 5, 6}, {2, 4, 5, 6}, {1, 2, 4, 5, 6},
{3, 4, 5, 6}, {1, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {1, 2, 3, 4, 5, 6} }
|
|