(A) Problem Statement :
Write a program to find all subsets of the given set.
(B) Input and Output:
(i)Enter no of elements in the set :3
enter set element : c
enter set element :d
enter set element : e
All subsets are:
empty
e
d
de
c
ce
cd
cde
(ii)Enter no of elements in the set :3
enter set element : a
enter set element :b
All subsets are:
empty
a
b
ab
(C) Examples :
(i) if the set contains {a,b,c} then the subsets of the given
set is
{Ø},{a},{b},{c},{a,b},{a,c},{b,c},{a,b,c}.
(ii) if set is {4,5} then the subsets of given set is
{Ø},{4},{5},{4,5}.
(D)Algorithm :
Step 1: Start
Step 2: read the no of elements of set n and read set
Elements, take Boolean array b of size as set and
initialize with 0’s..
Step 3: repeat the following steps 4 to 6 for I value 0 to n
Step 4: assign nth value in b to 1.
Step 5:if n value is less than n-1 then print subset
Step 6:repeat the step 3 to 5 for i+1
Step 7:stop
(E) Program :
#include
#include
#define SIZE 32
void show_subset(bool b[], char c[]);
void rec_iterate(bool b[],int n, char c[]);
void main()
{
char c[32];
int i,n;
bool b[SIZE]={0}; //read the no of elementsin set
printf("\n enter no of elemtns in the set");
scanf("%d",&n);
printf("\nenter elements into the set:");
for(i=0;i
printf("\n enter element:");
scanf("%c",&c[i]);
}
printf(“\n All subsets are:”);
rec_iterate(b,0,c); //call iterate functions
}
void show_subset(bool b[], char c[])
{
bool empty = true; //intialize empty to true
for(int i = 0; i < SIZE; ++i)
if(b[i] == 1) // if subset is found
{
empty = 0; // print subset
printf("%c",c[i]);
}
if(empty) //nullset is subset for all sets
printf("\nempty"); //print null set as empty
}
void rec_iterate(bool b[],int n, char c[])
{
for(int i = 0; i <= 1; ++i)
{
b[n] = i; //intialize with i value
if(n < SIZE -1)
rec_iterate(b, n + 1,c); //call iterate function
else
show_subset(b, c); //if subset found then
} //print by calling show_subset function
}
(F) Testing :
The program can be tested for the given set of elements
and for all its subsets.
(G) Limitations :
(i) this program can find power set of given set of size 32
only.
No comments:
Post a Comment