Advertisement
Mr_kindle

subset.c

Nov 14th, 2022 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. /*This code print all the subset of a given set*/
  2.  
  3.  
  4. #include<stdio.h>
  5. #include<math.h>
  6. #include<malloc.h>
  7. //#define ARRAYSIZE(x) sizeof(x)/sizeof(x[0])
  8. int main()
  9. {  
  10.     int *set;
  11.     int arraysize,i,j,count;
  12.     printf("Enter number of elements in array: ");
  13.     scanf("%d",&arraysize);
  14.    
  15.     set = calloc(arraysize, sizeof(int));
  16.     /*filling the array elements by users*/
  17.     printf("\nNow enter all the elements which must be unique: \n");
  18.     for(i=0;i<arraysize;i++)
  19.         {
  20.             printf("Enter element %d: ",i+1);
  21.             scanf("%d",&set[i]);
  22.         }
  23.     for(i=0;i<pow(2,arraysize);i++)
  24.         {   count=0;
  25.             printf(" ( ");
  26.             for(j=0;j<arraysize;j++)
  27.     /*if array has 'n' elements then check all 'n' bits of i (from 0 to n-1) either it is set or not*/
  28.     /*if jth bit of i is set then array[j] will be printed as subset*/
  29.                 if((1<<j) & i)
  30.                     {   /*if there is more than 1 element in a subset then put ',' in between them*/
  31.                         if(count>0)
  32.                             printf(",");
  33.                         count++;
  34.                    
  35.                          printf("%d",set[j]);
  36.                     }
  37.                   printf(" ),");
  38.         }printf("\b ");
  39.      
  40.      
  41. free(set);
  42. return 0;
  43. }//main
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement