3

I have a 1D array of ints:

int[] array = { 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32,33, 34,40,41,42,43, 44};

I would like to divide this 1D array into a 2D array of 4 rows and 5 columns, where the first 5 values goes into row 1, the next 5 in row 2 and so on. The final result should look like this:

array2D:

[[10, 11, 12, 13, 14]
[20, 21, 22, 23, 24]
[30, 31, 32, 33, 34]
[40, 41, 42, 43, 44]]

In reality the array will be much longer(maybe 100+ rows), but the number of columns is 5 and number of rows is dividable by 5. I have simplified for example. This is what I have tried so far:

int[] array = { 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32,33, 34,40,41,42,43, 44};
int[,] array2D = new int[(array.Length/5),5];

int count_0 = 1;
int count_1 = 1;
int count_2 = 1;
int count_3 = 1;
int count_4 = 1;

for (int i = 0; i < array.Length; i++)
{
    if (i < 5)
    {
        // works for the first 5 values: 
        array2D[0, i] = array[i];
    }
    // I tried this approach for the rest where I try to say that if Im at index 5, 
    //and every 5th element from here, append to it to index[i,0] of array2D and so on. 
    // This do not work, and is what I need help with.
    else if (i >= 5 && i % 5 == 0)
    {
        array2D[count_0, 0] = array[i];
        count_0++;
    }
    else if (i >= 6 && i % 5 == 0)
    {
        array2D[count_1, 1] = array[i];
        count_1++;
    }

    else if (i >= 7 && i % 5 == 0)
    {
        array2D[count_2, 2] = array[i];
        count_2++;
    }

    else if (i >= 8 && i % 5 == 0)
    {
        array2D[count_3, 3] = array[i];
        count_3++;
    }
    else if (i >= 9 && i % 5 == 0)
    {
        array2D[count_4, 4] = array[i];
        count_4++;
    }
}

Of course for this example I could just say if > 5 && <10 {append to array2D} and if > 10 && <15 {append to array2D} and so on, but I want something that works for a large array of hundreds of values. If someone has a smarter way to do this please let me know.

Thank you for any help!

3

3 Answers 3

8

You can just calculate the indexes:

for(int i=0;i<array.Length;i++)
    array2D[i/5, i%5] = array[i];
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that was an easy way to do it!
miracle of %(mod)
3

You can calculate the indexes easily using simple division. The row is calculated by the dividing i with the wanted column numbers. The column is simply the reminder of that division.

using System;

public class Program
{
    public static T[,] SplitInto2DArray<T>(T[] array, int rows, int columns) {      
        T[,] result = new T[rows, columns];

        for (int i = 0; i < array.Length; i++) {
            result[i / columns, i % columns] = array[i];    
        }

        return result;
    }

    public static void PrintArray<T>(T[,] array) {
        for (int i = 0; i < array.GetLength(0); i++) {
            for (int j = 0; j < array.GetLength(1); j++) {
                Console.Write(array[i, j] + " ");
            }
            Console.WriteLine();
        }
    }

    public static void Main()
    {
        int[] array = { 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40, 41, 42, 43, 44};

        int[,] splittedArray = SplitInto2DArray(array, 4, 5);

        PrintArray(splittedArray);
    }
}

2 Comments

While your answer may solve the issue, it would be helpful if you added some text describing the code.
Got it and added some explanation. Hope that's okay now. I'm new here :)
3

You can accomplish with LINQ using Enumerable.GroupBy.

array.GroupBy(x => x / 5)
   .Select(x => x.ToArray())
   .ToArray()

2 Comments

Not correct. This would only work assuming that the input array contains sequential integers, because x here is the actual value contained in the Array
thanks for revisiting this @pixelpax I believe you are right. I didn't see an overload of GroupBy containing element index, which in this case would substitute for x / 5 to return the row number as intended by my answer. Looking around stackoverflow.com/a/1290459/4846648 provides a possible approach to using groupby after selecting array as a tuple of elements and row index.

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.