-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsets_iterative.cpp
72 lines (63 loc) · 2.28 KB
/
subsets_iterative.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
// Given a set of {1, 2 .., n} integers, this code
// works out all the possible subsets of size k.
// ITERATIVE METHOD
// Compile with: g++ subsets_iterative.cpp -o subsets_ix -lm -O
// on Linux or Unix run with command ./subsets_ix
#include <iostream>
#include <cmath>
#include<limits>
using namespace std;
void print_array(unsigned long *A, unsigned long k, unsigned long j);
unsigned long increase_word(unsigned long *A, unsigned long n, unsigned long k);
int main(void)
{
unsigned long i=0, g=0, *A, n=0, k=1,j=0;
bool cycle =1; /*Value of "cycle" decideds whether to continue looping*/
while(k>n)
{
printf("Enter a value for N: "); /*Size of set*/
scanf("%ul", &n);
printf("Enter a value for K: "); /*Size of subsets*/
scanf("%ul", &k);
}
printf("The chosen values are: N= %u, K= %u\n\n",n,k);
A=new unsigned long [k]; /*A= Array to hold the subsets*/
j=1; /*j= Counts the number of subsets computed*/
for(i=0;i<k;++i) /*A[] is initialised with the trivial subset*/
A[i]=i+1;
print_array(A,k,j);
do{
g=increase_word(A,n,k); /*Checks 2C if an entry needs updating in the array*/
if(g==0) /*g= current value of A[k-1]*/
cycle=0;
else
{
for(i=g;i<n+1;++i){
A[k-1]=i;print_array(A,k,j);++j;}
}
} while (cycle ==1);
printf("\n\n%u Choose %u = %u\n",n,k,j);
delete A;
return(0);
}
void print_array(unsigned long *A,unsigned long k, unsigned long j)
{
unsigned long i;
printf("{");
for(i=0;i<k-1;++i)
printf("%u, ", A[i]);
printf("%u} subset number= %u \n",A[i],j);
}
unsigned long increase_word(unsigned long *A, unsigned long n, unsigned long k)
{
unsigned long i;
for(i=k;i>0;--i) /*Starting from A[k-1], entries are tested for increase*/
if(A[i-1]<(n+i-k)){
A[i-1]+=1;break;}
if(i==0)
return(0);
else
for(i=i;i<k;++i) /*If an increase was made to A[i]-1 for some i, A[i]->A[k-1] are reset*/
A[i]=A[i-1]+1;
return(A[k-1]);
}