Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
We get non negative integer number n from user and we must print all subsets of set ({1,2,3,...,n}).

for example for n=3 we must print:

{1 , 2 , 3}
{1 , 2}
{1 , 3}
{1}
{2 , 3}
{2}
{3}
{}
,s are optional and the sequence can be printed without any comma. (like {1 2 3}) I must add that the sequence of subsets must be exactly like the example. Meaning first the subsets that have 1, then subsets that have 2 and .... The longest subset must be printed first.

What I have tried:

I see a lot of codes in the Internet that solve this problem with arrays or using a bit array that indicate whether we use a number or not. The issue is that in this question, we are not allowed to use -any- type of array or other data structures like vector ,etc. Even using the array behaviour of something like string is completely prohibited. It must be solved only with recursion.

We are also not allowed to use any advanced functions. For example if we write it with C, we are allowed just to use stdio.h or for C++, only <iostream> is allowed and no other library.

I don't know how to do this without any arrays. How to check which number it must print and at the sametime, manage the {}.
Posted
Updated 28-Oct-18 7:50am

You must write code for a function which is outputing the actual data and than recursive call that function for all subsets in which one item is removed.

But how are you allowed to store that input? If in doubt ask your teacher!!!
 
Share this answer
 
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

The whole idea is to get you to think about recursion and how it works - and solving this is a big part of you understanding it properly. Getting handed something you can hand in doesn't do that.
Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Member 14035676 28-Oct-18 12:52pm    
I don't want you to solve it completely. At least give me some idea about how I do this. I can understand the array recursive methods used in internet, but in this question, it is completely prohibited to use array. I don't know how to handle which number it must print. For example in the array methods, sometimes they use a bit array that say whether we must use an element or not. But I don't know how to do this here without any array.
OriginalGriff 28-Oct-18 13:02pm    
That's because you don't understand how recursion works.
Try starting smaller, and write a recursive factorial function that prints each trem, and the result. That should give you clues.
Quote:
I see a lot of codes in the Internet that solve this problem with arrays or using a bit array that indicate whether we use a number or not. The issue is that in this question, we are not allowed to use -any- type of array or other data structures like vector ,etc.

Since any variable is also a bit array, there is not solution while respecting all constraint.
Quote:
I don't know how to do this without any arrays.

Start by solving with arrays, it will give you a starting point to think on how to get rid of arrays and what to make recursive. At least you will have a working solution.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900