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!

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
agrom
  • 386
  • 2
  • 5
  • 19
  • https://stackoverflow.com/questions/11207526/best-way-to-split-an-array – Drag and Drop Sep 21 '18 at 13:21
  • [1](https://stackoverflow.com/questions/3210824/divide-array-into-an-array-of-subsequence-array), [2](https://stackoverflow.com/questions/18986129/c-splitting-an-array-into-n-parts), [3](https://stackoverflow.com/questions/47683209/split-array-into-chunks-is-there-faster-method), [4](https://stackoverflow.com/questions/11463734/split-a-list-into-smaller-lists-of-n-size) – Drag and Drop Sep 21 '18 at 13:23
  • Choose your dupe target, I got at least 10+ of eligible one. – Drag and Drop Sep 21 '18 at 13:23

3 Answers3

8

You can just calculate the indexes:

for(int i=0;i<array.Length;i++)
    array2D[i/5, i%5] = array[i];
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
3

You can accomplish with LINQ using Enumerable.GroupBy.

array.GroupBy(x => x / 5)
   .Select(x => x.ToArray())
   .ToArray()
Matthew Thurston
  • 720
  • 5
  • 22
  • 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 – pixelpax Jul 06 '22 at 03:00
  • 1
    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 https://stackoverflow.com/a/1290459/4846648 provides a possible approach to using groupby after selecting array as a tuple of elements and row index. – Matthew Thurston Jul 13 '22 at 05:08
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);
    }
}
kubistika
  • 96
  • 3