C Program to Generate All Possible Subsets using Binary Counting Method

This is a C Program to implement binary counting method to generate subset of a set. This program generates all subsets of given set of numbers using binary counting method

Here is source code of the C Program to Implement the Binary Counting Method to Generate Subsets of a Set. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<math.h>
  3. int i, j;
  4. int sequence[] = { 9, 3, 5, 6, 7 };
  5.  
  6. void binaryCounting(int N) {
  7.     int binary[(int) pow(2, N)];
  8.     for (i = 0; i < pow(2, N); i++) {
  9.         int b = 1;
  10.         binary[i] = 0;
  11.         int num = i;
  12.         while (num > 0) {
  13.             binary[i] += (num % 2) * b;
  14.             num /= 2;
  15.             b = b * 10;
  16.         }
  17.     }
  18.     printf("\nThe permutations are: ");
  19.     for (i = 0; i < pow(2, N); i++) {
  20.         printf("{ ");
  21.         for (j = 0; j < N; j++) {
  22.             if (binary[i] % 10 == 1)
  23.                 printf("%d ", sequence[j]);
  24.             binary[i] /= 10;
  25.         }
  26.         printf("}\n");
  27.     }
  28. }
  29.  
  30. int main(int argc, char **argv) {
  31.     printf("The elements in the set : ");
  32.     for (i = 0; i < 5; i++)
  33.         printf("%d ", sequence[i]);
  34.  
  35.     for (i = 1; i < 5; i++) {
  36.         int j = i;
  37.         int temp = sequence[i];
  38.         while (j > 0 && temp < sequence[j - 1]) {
  39.             sequence[j] = sequence[j - 1];
  40.             j = j - 1;
  41.         }
  42.         sequence[j] = temp;
  43.     }
  44.     binaryCounting(5);
  45.     return 0;
  46. }

Output:

$ gcc0 BinaryCounting.c
$ ./a.out
 
The elements in the set : 9 3 5 6 7 
The permutations are: { }
{ 3 }
{ 5 }
{ 3 5 }
{ 6 }
{ 3 6 }
{ 5 6 }
{ 3 5 6 }
{ 7 }
{ 3 7 }
{ 5 7 }
{ 3 5 7 }
{ 6 7 }
{ 3 6 7 }
{ 5 6 7 }
{ 3 5 6 7 }
{ 9 }
{ 3 9 }
{ 5 9 }
{ 3 5 9 }
{ 6 9 }
{ 3 6 9 }
{ 5 6 9 }
{ 3 5 6 9 }
{ 7 9 }
{ 3 7 9 }
{ 5 7 9 }
{ 3 5 7 9 }
{ 6 7 9 }
{ 3 6 7 9 }
{ 5 6 7 9 }
{ 3 5 6 7 9 }

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.