Solved

Program for preparing sets from the given data

Posted on 2003-02-26
Medium Priority
182 Views
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
Question by:ydramu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 8

Expert Comment

ID: 8024947
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

LVL 8

Expert Comment

ID: 8024958
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

Expert Comment

ID: 8025409
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

LVL 8

Expert Comment

ID: 8025480
>>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

LVL 8

Expert Comment

ID: 8025495
Banath
in his question , i guess the asker mentioned that he needs the logic and not full source .. sorry for ignoring that comment..
0

LVL 8

Expert Comment

ID: 8025513
>>>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

LVL 7

Author Comment

ID: 8070680
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;

}
and thanks to one and all who suggested me
0

LVL 20

Expert Comment

ID: 10018994
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.

jmcg
EE Cleanup Volunteer
0

Accepted Solution

SpazMODic earned 0 total points
ID: 10050505
PAQed, with points refunded (100)

SpazMODic
EE Moderator
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to useā¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soā¦
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month12 days, 4 hours left to enroll