This content has been marked as final.
Show 7 replies

1. Re: All possible combinations given a 2D Array
699554 Jun 18, 2010 8:47 AM (in response to 843789)Waterborne wrote:
Permutations.
I want to find out all the possible combinations of it.
I currently am using recursion however, I am having difficulty in finding the correct way to do it.
Can't say I can help unless you explain to us the difficulties your facing, possibly post your initial attempts.
Mel 
2. Re: All possible combinations given a 2D Array
843789 Jun 18, 2010 9:19 AM (in response to 843789)Melanie_Green wrote:
At least the example shows combinations. The are 2^3 = 8 of them. If you look at the second index on each row, it goes like this,Waterborne wrote:
Permutations.
I want to find out all the possible combinations of it.
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
These are the 8 combinations of 2 values (0 and 1) in 3 positions.
The first index goes like this on every row,
0 1 2
representing the 3 positions.
From what I can see everything here can be easily generated using nested loops. The loops can be fixed if their number is known beforehand, or called recursively to the required nested depth if not. 
3. Re: All possible combinations given a 2D Array
843789 Jun 18, 2010 9:25 AM (in response to 843789)Hi Melanie and Wastrel, thanks for replying:
Yes, I am using recursion since the num of items and possible values may change.
I am having trouble with iterating the third:
Let's say it looks like this:
[0, 0]
> [1, 0]
> [2, 0]
> [2, 1]
>[1, 1]
> [2, 0]
> [2, 1]
Since I am adding them into only 1 String (ex. string input = [0,0] + [1,0] + [2,0]), I am returning the final. Problem is, my looping of the 2nd and 3rd iterations exit because I return. Therefore I do not get [2,1] and [1,1].
Thank you. 
4. Re: All possible combinations given a 2D Array
843789 Jun 18, 2010 9:40 AM (in response to 843789)Waterborne wrote:
I suggest you first develop an iterative solution based on nested loops. Each position will be one nesting and the loops will iterate the number of values. Then you generalize this to a recursive solution.
Hi Melanie and Wastrel, thanks for replying:
Yes, I am using recursion since the num of items and possible values may change.
The recursive function will have one loop only. Each recursive call will represent one level down the loop nesting in the iterative solution. 
5. This Thread is now moved
darrylburke Jun 18, 2010 10:30 AM (in response to 843789)Note: This thread was originally posted in the [Java Programminghttp://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment. 
6. Re: All possible combinations given a 2D Array
843789 Jun 18, 2010 3:00 PM (in response to 843789)Hi again, thanks for the reply.
I have no problem making it in iteration: Given a 2D array of [3] [index], I just do this:
int m = 0; for(int a = 0; a < index; a++) { for(int b = 0; b < index; b++) { for(int c = 0; c < index; c++) { System.out.println("TEST: " + possibledecrypted[m][a] + " " + possibledecrypted[m+1] + " " + possibledecrypted[m+2][c]);
}
}
}
String PrintKey(String[][] possibledecrypted, int m, int maxnumber, int index)I picture this in recursion as:
{
if(m == maxnumber)
{
return possibledecrypted[m][curindex];
}
else
{
for(int m = 0; m < index; m++)
{
System.out.println("TEST: " + possibledecrypted[m][a] + " " + PrintKeyAgain(possibledecrypted, m + 1, maxnumber, index));
}
}
}
Problem is, the above code does not work properly at all. It does not get all the proper combinations. Also, it just prints 2 number of items. Please help me, recursions always blow my head off! Thank you!

7. Re: All possible combinations given a 2D Array
843789 Jun 18, 2010 4:10 PM (in response to 843789)Hi all,
I finally solved this problem. Here is the recursive printing of the values:
public static void PrintPossibleKeys(String[][] possibledecrypted, int i, int j, int max, int index, String curstring) { if((i+1) == max) { for(int c = 0; c < index; c++) { System.out.println("Test: " + curstring + " " + possibledecrypted[i][c]); } } else { for(int b = 0; b < index; b++) { PrintPossibleKeys(possibledecrypted, (i+1), 0, max, index, (curstring) + " " + possibledecrypted);
}
}
}
The main idea I used is to collectively collect the strings traversed and pass it as a parameter then print everything out once it reaches the final. Special thanks to wastrel for giving me tips on how to create recursions (first try iterative solution). Thank you all!