Printing combination of given size from given array


Problem statement
Write a c program to first take inputs from user, then print all the possible sub set of given size(the size will also be given by user)


#include<stdio.h>
void ncrenum(int data[],int comb[],int combind,int n,int r);
void printset(int comb[], int r);
int main()
{
  int total,i,r;
  int data[100],comb[100],combind=0;
  printf("enter the numbr of values ");
  scanf("%d",&total);
  printf("now enter %d numbers \n",total);
  for(i=0;i<total;i++)
    {
      scanf("%d",&data[i]);    
    }
  printf("enter the number which one set will contain\n");
  scanf("%d",&r);
  ncrenum(data,comb,combind,total,r);    
  return 0;
}
void ncrenum(int data[],int comb[],int combind,int n,int r)
{  
  int i;
  if(r==0||n==r)
    {
      if(r==0)
{
 printset( comb,combind);
 return ;
}
      if(n==r)
{
 for(i=0;i<n;i++)
   {
     comb[combind]=data[i];
     combind++;
   }
 printset(comb,combind);
 return ;        
}
    }
  ncrenum(data,comb,combind,n-1,r);
  comb[combind]=data[n-1];
  combind=combind+1;
  ncrenum(data,comb,combind,n-1,r-1);
  return ;
}
void printset(int comb[], int r)
{
  int m;
  for(m=0;m<r;m++)
    printf("%d ",comb[m]);
  printf("\n");
  return ;
}

No comments:

Post a Comment