Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

All combinations of an int array

 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic