agrom
agrom

Reputation: 396

C# split 1D array into 2D array for every Nth value

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!

Upvotes: 3

Views: 1212

Answers (3)

Matthew Thurston
Matthew Thurston

Reputation: 760

You can accomplish with LINQ using Enumerable.GroupBy.

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

Upvotes: 3

kubistika
kubistika

Reputation: 96

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);
    }
}

Upvotes: 3

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34150

You can just calculate the indexes:

for(int i=0;i<array.Length;i++)
    array2D[i/5, i%5] = array[i];

Upvotes: 8

Related Questions