Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*This code print all the subset of a given set*/
- #include<stdio.h>
- #include<math.h>
- #include<malloc.h>
- //#define ARRAYSIZE(x) sizeof(x)/sizeof(x[0])
- int main()
- {
- int *set;
- int arraysize,i,j,count;
- printf("Enter number of elements in array: ");
- scanf("%d",&arraysize);
- set = calloc(arraysize, sizeof(int));
- /*filling the array elements by users*/
- printf("\nNow enter all the elements which must be unique: \n");
- for(i=0;i<arraysize;i++)
- {
- printf("Enter element %d: ",i+1);
- scanf("%d",&set[i]);
- }
- for(i=0;i<pow(2,arraysize);i++)
- { count=0;
- printf(" ( ");
- for(j=0;j<arraysize;j++)
- /*if array has 'n' elements then check all 'n' bits of i (from 0 to n-1) either it is set or not*/
- /*if jth bit of i is set then array[j] will be printed as subset*/
- if((1<<j) & i)
- { /*if there is more than 1 element in a subset then put ',' in between them*/
- if(count>0)
- printf(",");
- count++;
- printf("%d",set[j]);
- }
- printf(" ),");
- }printf("\b ");
- free(set);
- return 0;
- }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement