#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20
#define YES 1
#define LOOP 1
#define FLIP_DIGIT(x) x = ((x) == '0' ? '1' : '0')
#define FLIP(x) x = (1 - (x))
void main(void)
{
char digit[MAXSIZE];
int position[MAXSIZE];
int even;
int n;
int i, count = 0;
char line[100];
printf("\nAll Subset Listing by Gray Code");
printf("\n===============================");
printf("\n\nNumber of Elements in Set Please --> ");
gets(line);
n = atoi(line);
printf("\n"); /* initialization */
for (i = 0; i < n; i++) {
digit[i] = '0';
printf("0");
}
printf(" : {}\n"); /* the empty set */
even = YES;
while (LOOP) {
if (even) /* for even positions:0,2,..*/
FLIP_DIGIT(digit[0]); /* flip the 1st bit */
else { /* for odd positions... */
for (i = 0; i < n && digit[i] == '0'; i++)
; /* find the first 1 bit */
if (i == n-1) break; /* if it is the last..*/
FLIP_DIGIT(digit[i+1]); /* NO, flip its nbr*/
}
//output
for (count = 0, i = n - 1; i >= 0; i--) {
printf("%c", digit[i]); /* print the bits */
if (digit[i] == '1') /* and collect pos */
position[count++] = i + 1;
}
printf(" : {%d", position[count-1]);
for (i = count - 2; i >= 0; i--) /* print pos */
printf(",%d", position[i]);
printf("}\n");
FLIP(even); /* next will be odd(or even)*/
}
}