Program to Print Power Set of Numbers

Problem Statement
Write a c program to print all the possible set of  numbers
you should take input from the user
If S is a set of n elements, the power set of S is the set of all possible subsets of S. For
example, if S = {a, b, c} , then power set(S) =  , {a} , {b} , {c} , {a, b} , {a, c }, {b, c} , {a, b, c }.
Write a recursive function to print the power set of a set of characters which are stored in
an array

#include<stdio.h>
#include<stdlib.h>
void print(int b[],int tos);
void per(int a[],int b[],int tos,int size,int index);
int main()
{
  int *a,*b,size,tos,y,i;
  tos=0;
  printf("enter the nmbers of chars \n");
  scanf("%d",&size);
  a=(int*)malloc(size*sizeof(int));
  b=(int*)malloc(size*sizeof(int));
  printf("enter %d chars now \n",size);
  for(i=0;i<size;i++)
    {
      scanf("%d",&a[i]);
    }
  printf("here is the possible permutations \n");
  per(a,b,tos,size,0);
  scanf("%d",&y);
  return 0;
}
void per(int a[],int b[],int tos,int size,int index)
{
  if(index==size) 
    {
      print(b,tos);
      return ; 
    }
  per(a,b,tos,size,index+1);
  b[tos]=a[index];
  per(a,b,tos+1,size,index+1);
  return ; 
}
void print(int b[],int tos)
{
  int i;
  if(tos==0)
    {
      printf("{no }\n");
      return ;
    }
  printf("{");
  for(i=0;i<tos;i++)
    {
      printf("%d ",b[i]);
    }
  printf("}\n"); 
}

No comments:

Post a Comment