3

i have let's say the following arrays..

int[] array_1 = new int[1] { 2 };
int[] array_2 = new int[2] { 3, 4 };
int[] array_3 = new int[3] { 7,5, 6 };
int combinations;
combinations = array_1.Length * array_2.Length * array_3.Length;

what i want to do is to create a new array where each column will contain elements from each of the above arrays and the number of rows will be the combination of all elements of the above array. in this case the number of columns is 3 (because i have 3 arrays) and the number of columns is 6, as combinations of all elements are 1*2*3=6.So, my new array is going to be:

int array_num = 3;
int[,] comb = new int[combinations, array_num];

I want to fill each column in the following way: The first column will contain elements from the array_1 array which will change every

int c0 = (combinations / array_1.Length);

elements. the second column will contain elements from the array_2 which will change every

int c1=(c0/array_2.Length);

elements. And the third column will contain elements from thw array_3 which will change every

int c2=(c1/array_3.Length);

elements. For this particular example the result comb array will be this:

2     3     7    
2     3     5
2     3     6
2     4     7
2     4     5
2     4     6

I hope i made my problem clear and I'm looking forward to any suggestions about how should i start, since I'm new in programming.

4
  • 1
    OK,. sounds like you're fairly well thought out, so, what have you tried so far? Commented Jun 1, 2011 at 12:03
  • i've tried a for loop to pass elements from the first array to the the first column of the comb..using the c0 value to check where the elements should change..but i'm not sure about the way i should procede in the next column...you see i denoted 'comb[j,i]=array_1[0]'in the loop..and the concept is that you don't know in advance the number of arrays or the number of the elements in each array.. Commented Jun 1, 2011 at 12:09
  • OK, well as people have provided the answer, I guess you wont get to work out why it works, but.. in essense, for each element in array 1, go through each element in array 2, and for each element in array 3, create a new array entry of the element from array1, array2, array3.. which is exactly what you did when you wrote it down. Commented Jun 1, 2011 at 12:22
  • thank you all guys for the answers..I'll process them and return with the solution(I hope..)thanks again!!! Commented Jun 1, 2011 at 12:45

4 Answers 4

1
    static int[,] Combine(params int[][] arrays)
    {
        int[][] combined = CombineRecursive(arrays).Select(x=>x.ToArray()).ToArray();
        return JaggedToRectangular(combined);
    }

    static IEnumerable<IEnumerable<int>> CombineRecursive(IEnumerable<IEnumerable<int>> arrays)
    {
        if (arrays.Count() > 1)
            return from a in arrays.First()
                   from b in CombineRecursive(arrays.Skip(1))
                   select Enumerable.Repeat(a, 1).Union(b);
        else
            return from a in arrays.First()
                   select Enumerable.Repeat(a, 1);
    }

    static int[,] JaggedToRectangular(int[][] combined)
    {
        int length = combined.Length;
        int width = combined.Min(x=>x.Length);
        int[,] result = new int[length, width];

        for (int y = 0; y < length; y++)
            for (int x = 0; x < width; x++)
                result[y, x] = combined[y][x];

        return result;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

You are basically trying to reshape 3D positions into a 2D array, you need to calculate the 2D position from the 3D position and copy the values.

Use for loops, e.g.

for (int dim0 = 0; dim0 < array1.Length; dim0++)
{
    for (int dim1 = 0; dim1 < array2.Length; dim1++)
    {
        for (int dim2 = 0; dim2 < array3.Length; dim2++)
        {
            var pos = (dim0 * array2.Length + dim2) * array3.Length + dim2;
            var pos0 = pos / array2.Length / array2.Length;
            var pos1 = pos % (array2.Length * array2.Length);
            matrix[pos0, pos1] = ...
        }
    }
}

Comments

1
    static int[,] Combine(params int[][] arrays)
    {
        int[] pointers = new int[arrays.Length];

        int total = arrays.Aggregate(1, (m, array) => m * array.Length);
        int[,] result = new int[total, arrays.Length];

        for (int i = 0; i < arrays.Length; i++)
            result[0, i] = arrays[i][0];

        for (int y = 1; y < total; y++)
            for (int x = arrays.Length - 1; x >= 0; x--)
            {
                ++pointers[x];
                for (int i = x; i >= 0; i-- )
                    if (pointers[i] >= arrays[i].Length)
                    {
                        pointers[i] = 0;
                        if (i>0)
                            pointers[i - 1]++;
                    }
                result[y, x] = arrays[x][pointers[x]];
            }

        return result;
    }

Comments

0
for i = 0 to arraysize1
{
    for j = 0 to arraysize2
    {
        for k = 0 to arraysize3
        {

            array1[i] , array1[j] , array1[k] is what you are looking for

        }

    }
}

As you can see the the loop will run arraysize1 * arraysize2 * arraysize3 and generate all combinations for the case you just listed.

If the number of arrays are not known ahead of time. You will have to learn about recursion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.