Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

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.
0
ydramu
Asked:
ydramu
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now