Arrays

Jan 21, 2017 at 5:55pm
Hi there,

I have been trying to create a program which prints the elements of an array in combinations.

Is there any way on how to go with that?

Thanks and regards
Jan 21, 2017 at 7:24pm
What combinations? All subsets?
Jan 22, 2017 at 5:00pm
yes. exactly. all subsets.
Given a set with 'n' elements all possible subsets. In case that it helps, below what I ve done so far

#include<iostream>
using namespace std;

void Combs(int B[], int n, int y)
{
int D[4];
for (int j = 0; j <= n; j++)
{
D[0] = B[j];


cout << "\n";
for (int i = 0; i < y; i++)

{
cout << B[i];
}

// Combs(D, n, y+1);
y++;


}
}

int main() {
int y = 0;
int n;
cout << "Enter the n\n";
cin >> n;
int *B = new int[n]; for (int i = 0; i <n; i++) B[i] = i;
cout << "All possible subsets are :\n";
Combs(B, n, y);
cout << endl;
system("pause");
}
Jan 22, 2017 at 9:33pm
It's a rather common problem, you can also do it in an iterative way:
http://www.geeksforgeeks.org/power-set/
Jan 22, 2017 at 11:21pm
Just a comment: "Combinations" means something entirely different than "all possible subsets". As @GoldenLizard's link suggests, you're looking for the "power set".
Jan 23, 2017 at 6:16pm
Thank you both for the replies.
Yeah, you re right. In fact I am looking for the power set.
However, the one suggested is not exactly what i m looking for.
What i need to do is to print the subsets based on the index of the array recursively.
Topic archived. No new replies allowed.