-
Notifications
You must be signed in to change notification settings - Fork 17
/
ds question 1.cpp
151 lines (131 loc) · 2.21 KB
/
ds question 1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include<iostream>
#include<math.h>
using namespace std;
class set
{
int *ar;
int size;
public:set()
{
size=0;
ar=new int[size];
}
void getinput();
void print();
void cardinality();
void is_member();
void power_set();
};
void set::getinput()
{
cout<<"enter the size of an array ";
cin>>size;
ar=new int[size];
cout<<"enter the elements "<<endl;
for(int i=0;i<size;i++)
cin>>ar[i];
}
void set::print(){
cout<<"the given set is";
cout<<"\nSET A : { ";
for(int i=0;i<size;i++)
{
if(i==0)
cout<<ar[i];
else
cout<<","<<ar[i];
}
cout<<" }"<<endl;
}
void set::cardinality()
{
cout<<"\nAfter removing redudancy ";
cout<<"\nSET A : {";int c=0;
for(int i=0;i<size;++i)
for(int j=i+1;j<size;)
{
if(ar[i]==ar[j])
{
for(int k=j;k<size-1;++k)
ar[k]=ar[k+1];
--size;
}
else
++j;
}
for(int i=0;i<size;++i)
if(i==0)
cout<<ar[i];
else
cout<<","<<ar[i];
cout<<"}"<<endl;
cout<<"\nCardinality "<<size;
}
void set::is_member()
{
int k,flag=0;
cout<<"\nEnter the number to be checked ";
cin>>k;
for(int i=0;i<size;i++)
{
if(ar[i]==k)
{
flag=1;
break;
}
}
if(flag==1)
cout<<"element is the member of setA\n";
else
cout<<"element is not the member of set A";
}
void set::power_set()
{
int count,temp;
cout<<"{ {},";
for(int i=0;i<size;++i)
for(int j=i+1;j<size;)
{
if(ar[i]==ar[j])
{
for(int k=j;k<size-1;++k)
ar[k]=ar[k+1];
--size;
}
else
++j;
}
count=pow(2,size);
for(int i=0;i<count;i++)
{
cout<<"{";
for(int j=0;j<size;j++)
{
if(i&(1<<j))
cout<<ar[j]<<",";
}
cout<<"\b},";
}
cout<<"\b}";
}
int main()
{
int ch;
set a;
a.getinput();
a.print();
cout<<"\n1.cardinality ";
cout<<"\n2. to check whether the element is member of set or not ";
cout<<"\n3. powerset ";
cout<<"\nenter your choice : ";
cin>>ch;
switch(ch)
{
case 1:a.cardinality();
break;
case 2:a.is_member();
break;
case 3:a.power_set();
break;
}
}