I want to generate all possible combinations from a 2D [m x n] array except for the first element of each array. That element will stand for the 'type' signifying the rest elements. For example, if I've an array
shirts[][] =
{
{"colour", "red", "blue", "green", "yellow"},
{"cloth", "cotton", "poly", "silk"},
{"type", "full", "half"}
};
The desired output should be combination of all the possibilities of shirt. For the above example,
colour red
colour blue
...
cloth silk
type full
type half
colour red cloth cotton
colour red cloth poly
...
colour yellow type half
cloth cotton type full
...
cloth silk type half
colour red cloth cotton type full
...
colour yellow cloth silk type half
I tried something like this (also took help from other Stack Overflow Question )
String shirts[][] =
{
{"colour", "red", "blue", "green", "yellow"},
{"cloth", "cotton", "poly", "silk"},
{"type", "full", "half"}
};
majorCombinations = new int[possibilities][shirts.length];
int currentCombination;
int offset = 1;
for (int i=0; i < shirts.length; i++)
{
currentCombination = 0;
while (currentCombination < possibilities)
{
for (int j=0; j < shirts[i].length; j++)
{
for (int k=0; k < offset; k++)
{
if (currentCombination < possibilities)
{
majorCombinations[currentCombination][i] = shirts[i][j];
currentCombination++;
}
}
}
}
offset *= shirts[i].length;
}
but it gives values of ALL n combinations only i.e.
colour cloth type
colour cloth full
...
yellow silk half
It doesn't take into account smaller combinations and it ain't even generic i.e. for an [m x n] array (n need not be fixed). A help in VBA would be highly appreciated. I'm comfortable with C, Java and C# as well. Thanks in advance :)