Link to home
Start Free TrialLog in
Avatar of ydramu
ydramuFlag for United States of America

asked on

Program for preparing sets from the given data

Hello friend,
  I have a problem which will store all the possible number of sets that are comming for given number of values. like if the input is 3. the 1,2,3 will form different combinations of sets.
like {{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}}. the total number of sets is (2 to the power of n)-1. here n is 3.
I want to store all these distinct combinations in a two dimensional array. So please provide me the logic for this as quickly as possible.

yours sincerely,
ramu.
Avatar of akshayxx
akshayxx
Flag of United States of America image

once i wrote a small program to make subsets of
"abcdef..z".. where u specify number of alphabets .. like if u input 3 u'll get a,b,c,ab,bc,ca,abc, and ofcourse NULL set..
u can modify that with just one string change..
i'll get u tomorrow . if by then u dont find any one..
right now away from my computer
by the way can u confirm this is not a home assignment .. otherwise i OR any other expert will not provide any source code..
btw the code i wrote will be a tricky one..to understand which u'll have to put some effort..
Avatar of Banath
Banath

ydramu, as suggested by akshayxx, this does look like homework. Our experts are forbidden from doing someone else's homework and may be punished for it.

What we do allow, for example, is general pointers, and showing where a mistake has been made.

If this is homework:
Please attempt to solve the problem yourself. If you have trouble with something *specific*, ask for help with that one issue.

If not, please demonstrate the need for this algorithm in your system.

Banath
EE Moderator
>>So please provide me the logic for this as quickly as possible.
logic  is pretty simple.. i'll give u something recursive..i hope u understand what recursion means..
here is pseudoCode
storageArrray can look like.
struct subsets{
char **subsets; //two dimensional array
int count;
};
subsets(storageArrayPointer,n){
  if n >1
    calculate  subsets(storageArrayPointer,n-1)
    for each subset in storageArrayPointer
     make pair n,subset
     increment storageArrayPointer->count
     store the pair as subset
  if n=1
    increment storageArrayPointer->couter
     store phy(null subset) as empty subset
    increment storageArrayPointer->couter
     store 1 as only non-empty subset
return
}

pair of n,given subset is defined as
 suppose u have n =3 and current subsets are
  {phy},1,2,12,
then pair are respectively
   3,13,23,123

pair of n,{phy} is 'n'

so in the storageArrayPointer .. u'll get 2^n subsets .. 2^n-1  + 1(empty subset)
phy is univseral subset
Banath
 in his question , i guess the asker mentioned that he needs the logic and not full source .. sorry for ignoring that comment..
>>>increment storageArrayPointer->count
>>>    store the pair as subset
where ever i have said this .. u need to increase the size of
storageArrayPointer->subsets .. using realloc..
and then make space for the string that will depict ur subset..
like to depict the subset {1,2,3}
u can use string "1,2,3"
and store it in the newly re-allocated space at the end
Avatar of ydramu

ASKER

hello friends I solved the myself.
#include<stdio.h>
#define n 5
int sets[50][5];
int initpos=0,finalpos=n,pointer=0,finalrow=n;
int length=2;
int base[n],i,j;
main()
{
   initialize();
   clrscr();
   while(1)
     {
     while(initpos<finalpos-1)
       {
          copy_previous();
                find_base_ptr(sets[finalrow][j-1]);

          while(1 && pointer<=n)
          {
            sets[finalrow][j]=base[pointer++];
            finalrow++;
            if (pointer>=n)
               break;
            copy_previous();
          }
           initpos++;
           if(sets[initpos][j]==base[n-1]) break;
        }
       length++;
       initpos=finalpos;
       finalpos=finalrow;
       if(length==n+1)
          break;
     }

     print();

     }
print()
{
 for(i=0;i<finalrow;i++){
   for(j=0;j<n;j++)
       printf("%d",sets[i][j]);
   printf("\n");
  }
}




copy_previous()

{
j=0;
while(j<length-1)
   sets[finalrow][j]=sets[initpos][j++];
}

initialize()
{
     for(i=0;i<n;i++)
        base[i]=sets[i][0]=i+1;
}

find_base_ptr(int x)
{
  for(i=0;i<n;i++)
     if(base[i]==x)
     pointer=i+1;

}
please try this.
and thanks to one and all who suggested me
Nothing has happened on this question in over 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
PAQ, refund points.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of SpazMODic
SpazMODic

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial