• C

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.
LVL 7
ydramuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

akshayxxCommented:
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
0
akshayxxCommented:
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..
0
BanathCommented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

akshayxxCommented:
>>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
0
akshayxxCommented:
Banath
 in his question , i guess the asker mentioned that he needs the logic and not full source .. sorry for ignoring that comment..
0
akshayxxCommented:
>>>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
0
ydramuAuthor Commented:
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
0
jmcgOwnerCommented:
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
0
SpazMODicCommented:
PAQed, with points refunded (100)

SpazMODic
EE Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.