# All combinations of an int array

S Ali
Ranch Hand
Posts: 129
Hi all,
I need to make a recursive function that can make me all possible combinations of an int array, I don't want to use a for loop because the size of the array is user input. Could anybody please guide me thought this because I have no idea how to implement a recursive function.
ex: int array[] {1,2,3,4}
possibilities :
0
1
2
3
4
12
13
14
123
1234
134
23
234
34
24
124

Gregg Bolinger
Ranch Hand
Posts: 15304
6
Do you know what a recursive function is? It's is a function that calls itself, basically. If you know how to write method in java, then having a method call itself is pretty easy. You just have to work out when it needs to stop calling itself so you don't end up in an infinite loop. The best thing for you do to is try something and then post that code here and we can help you figure it out. No one is going to write it for you.

Ireneusz Kordal
Ranch Hand
Posts: 423
Don't try to reinvent the wheel. Read this first:
http://www.cs.utsa.edu/~wagner/knuth/
The Art of Computer Programming Volume 4, Combinatorial Algorithms
chapter 7.2.1.3. Generating all combinations

David Newton
Author
Rancher
Posts: 12617
I disagree: *do* try to reinvent the wheel when you're learning something--otherwise you're just regurgitating somebody else's intelligence.

Fred Hamilton
Ranch Hand
Posts: 684
You might benefit from having a look at the recursive solution to the factorial problem. The factorial problem does have one or two things in common with your array problem.

Here is where I got my feet wet with recursion:

http://erwnerve.tripod.com/prog/recursion/recintro.htm

Henry Wong
author
Marshal
Posts: 21497
84

When it comes to recursion, I am definitely *not* a fan of learning by example.

If you look at a solution, in my opinion, at best, you just learn how that solution works. And if you are unlucky, you only think you know how it works.

Recursion requires a way of thinking out the problem. And if you never try to figure it out, with the easy examples, how can you do the hard ones? You need to mentally exercise your recursion muscles, on the easy problems, to be strong enough for the hard ones.

Henry

Fred Hamilton
Ranch Hand
Posts: 684
Henry Wong wrote:
When it comes to recursion, I am definitely *not* a fan of learning by example.

If you look at a solution, in my opinion, at best, you just learn how that solution works. And if you are unlucky, you only think you know how it works.

Recursion requires a way of thinking out the problem. And if you never try to figure it out, with the easy examples, how can you do the hard ones? You need to mentally exercise your recursion muscles, on the easy problems, to be strong enough for the hard ones.

Henry

I disagree, but you're the mod, so my opinion must be wrong. sigh. I knew there was a reason why I quit this board. Too many people who insist that their way is the best.

Henry Wong
author
Marshal
Posts: 21497
84
Fred Hamilton wrote:
I disagree, but you're the mod, so my opinion must be wrong. sigh. I knew there was a reason why I quit this board. Too many people who insist that their way is the best.

How does not being a fan of something impose on you? Or are you saying that you disagree, and that I am a fan?

Or are you saying that since I am a mod, I shouldn't give my opinion, because it means that everyone else is wrong?

Henry

David Newton
Author
Rancher
Posts: 12617
Fred Hamilton wrote:I disagree, but you're the mod, so my opinion must be wrong. sigh. I knew there was a reason why I quit this board. Too many people who insist that their way is the best.

Um... he even *said* "in my opinion". I don't get your reaction at all.