Integer subset generator

Submitted by:Andery Smith

Date added:22 November, 2015

Category:C++

To print out all possible subsets in lexicographic order

Tags: integer , generator , subset , lexicographic order

Code Snippet:

/*========================================
Given: An integer 'N'
Output: To print out all possible subsets
in lexicographic order
Notes: Excludes the trivial empty subset
{}
========================================*/
#include <iostream>

using namespace std;

int main()
{
int i, k=1, n;
int x[100];

cout<<"Enter N >>";
cin>>n;

x[1] = 1;

while (k)
{
for (i=1; i <= k; i++)
cout<<" "<<x[i];
cout<<"\n";

if (x[k] == n)
{
k--;
x[k]++;
}
else
{
k++;
x[k] = x[k-1] + 1;
}
}
return 0;
}
 
 

Comments