0

I have an array of integers, for example:

int[][] matrix = new int[][] { 
    new int[2],  // array a,
    new int[2],  // array b,
    new int[3],  // array c,
    new int[4],  // array d,
    // ...       // array x
};

Now i want to generate all the possible index Combination of select one from each array a, b, c, ... x

This means in the example of: {new int[2], new int[2], new int[3]},I want to get such index combination:

{a[0], b[0], c[0]}
{a[0], b[0], c[1]}
{a[0], b[0], c[2]}
{a[0], b[1], c[0]}
{a[0], b[1], c[1]}
{a[0], b[1], c[2]}
{a[1], b[0], c[0]}
{a[1], b[0], c[1]}
{a[1], b[0], c[2]}
{a[1], b[1], c[0]}
{a[1], b[1], c[1]}
{a[1], b[1], c[2]}

The length of the matrix is unknown, but every array in the matrix has at least 1 elements.

Does anyone have a solution for that?

treesong
  • 193
  • 7
Julian
  • 185
  • 1
  • 15
  • Please try first yourself. When you run into a specific question, feel free to ask here. SO is not a code writing service. – Sefe May 16 '17 at 08:27
  • I think this is not a code writing problem but more a problem finding an algorithm that works. The more i think about the problem the more i get confused. In my understanding it is not possible to use classic permutations to solve this. am i right? – Julian May 16 '17 at 08:31
  • @JulianHerbel: i dont understand it. Your array has three indexes, 0, 1 and 2. But your result array contains 1,2 and 3. The values of your source array don't matter at all? – Tim Schmelter May 16 '17 at 08:53
  • @TimSchmelter: Maybe my question was a little confusing. I want every possible combination of indexes in my array. Look at the answer of treesong. He solved it. Thanks again – Julian May 16 '17 at 09:29

2 Answers2

1

Here is the code.

class MainClass
{
    public static void Main(string[] args)
    {
        printAll(new int[] { 2, 2, 4});
        printAll(new int[] { 3, 3, 4, 5, 6 });
        printAll(new int[] { 3, 3, 4, 5, 6, 7, 8, 9});
    }

    public static void printAll(int[] array)
    {
        int max = 1;
        for (int i = 0; i < array.Length; i++)
        {
            max *= array[i];
        }

        for (int row = 0; row < max; row++)
        {

            int[] line = new int[array.Length];

            int weight = 1;
            for (int j = 0; j < array.Length; j++)
            {
                int times = row / weight;
                // line[j] = times % array[j]; // but you want to start form 1 
                line[j] = 1 + (times % array[j]); 
                weight *= array[j];
            }

            Console.WriteLine(String.Join(",", line));
        }
    }
}
treesong
  • 193
  • 7
  • Tested it as blackbox and it works for me. I dont understand it fully at the moment but i am trying to get into it. Thank you very much! Also i have to learn about other number systems in programming. Thanks for that hint! – Julian May 16 '17 at 09:27
  • Sorry for can't explain this much clear. The question equals to: https://stackoverflow.com/questions/1719594/iterative-cartesian-product-in-java – treesong Mar 23 '18 at 02:22
0

You can try this:

 List<int> list = new List<int>(){1, 2, 3};
        List<int> lstResult = new List<int>();
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                for (int z = 0; z < 3; z++)
                {
                    //Check if it exist before adding   
                    if (!lstResult.Contains(int.Parse(list[i].ToString() + list[i].ToString() + list[z].ToString())))
                    {
                        lstResult.Add(int.Parse(list[i].ToString() + list[i].ToString() + list[z].ToString()));
                    }
                }
                 //Check if it exist before adding
                if (!lstResult.Contains(int.Parse(list[i].ToString() + list[j].ToString() + list[i].ToString())))
                {
                    lstResult.Add(int.Parse(list[i].ToString() + list[j].ToString() + list[i].ToString()));
                }
            }
        }
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57