i want implement a function in scheme(Dr.Racket) that takes a simple list as inp
ID: 3766449 • Letter: I
Question
i want implement a function in scheme(Dr.Racket) that takes a simple list as input and returns the powerset of the list.let's Assume the input list as a set, and generate the powerset. A powerset is defined as a set of all subsets of a set. also, Assume that all of the elements of the input list are distinct. HINT: powerset of {a, b, c, d,...} is equal to union of powerset of {b, c, d,...} and 'a' appended to each element of powerset of {b, c, d,...}. The ordering of the sets in the powerset is not important. For example,
(powerset '(1 2 3))
yields or the output should be (() (1) (2) (3) (1 2) (1 3) (2 3) (1 2 3))
Explanation / Answer
<code>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter the size of set:";
cin >> n;
int arr[n];
vector<vector<int> > v;
vector<int>vv;
cout<<"Enter the elements of set:";
for(int i = 0;i < n;i++){
cin >> arr[i];
}
for(int i = 0;i < (1<<n);i++){
for(int j = 0;j < n;j++){
if (i>>j & 1){
vv.push_back(arr[j]);
}
}
v.push_back(vv);
vv.clear();
}
for(int i = 0;i < v.size();i++){
cout<< "(";
for(int j = 0;j < v[i].size();j++){
if (j == v[i].size() -1){
cout<<v[i][j];
}else{
cout<<v[i][j]<<",";
}
}
cout<< ")";
}
return 0;
}
</code>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.