1

How to write function to got new B array but put multiple 3 elements of array A..

int[] a = new int[] {1,2,3,4,5,6,7,8,9,10};

b0 = a1,a2,a3
b1 = a4,a5,a6 
b2 = etc...
Collateral.dmg
  • 128
  • 1
  • 10
sadux
  • 37
  • 5

5 Answers5

1

Well if you cannot use LINQ than simple for loop in an extension method can do the trick :

    public static int[][] GetChunks(int[] array, int chunkSize)
    {
        if(array.Length < chunkSize)
        {
           throw new ArgumentOutOfRangeException("chunkSize");
        }

        var result = new List<int[]>(array.Length / chunkSize);

        for (var i = 0; i < array.Length; i += chunkSize)
        {
            var chunk = new int[chunkSize + i < array.Length ? chunkSize : (array.Length - i - 1)];
            for (var j = 0; j < chunk.Length; j++)
            {
                chunk[j] = array[i + j];
            }
            result.Add(chunk);
        }

        return result.ToArray();
    }

Example of usage :

    int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    int[][] chunksOfFixedSize = GetChunks(nums, 3);

    Console.WriteLine("Number of chunks : " +  chunksOfFixedSize.Length);

    Console.WriteLine("Second element at 3rd chunk is : " + chunksOfFixedSize[2][1]);

Output :

Number of chunks : 4
Second element at 3rd chunk is : 8

P.S.

If you prefer to have different variables per each array you could simply do something like :

 var b = chunksOfFixedSize[0];
 var c = chunksOfFixedSize[1];
 var d = chunksOfFixedSize[2];
Fabjan
  • 13,506
  • 4
  • 25
  • 52
1

ONLY if you aren't allowed to use Linq...

int[] a = new int[] {1,2,3,4,5,6,7,8,9,10};
int[,] b;
if(a.Length % 3 != 0)
{
    b = new int[a.Length/3+1,3];
}
else
{
    b = new int[a.Length/3, 3];
}

for(int i = 0; i< a.Length;i++)
{
    b[i/3,i%3] = a[i];
}
Thomas
  • 2,093
  • 3
  • 21
  • 40
1

Here is a solution where you get the exact lengths in the arrays:

int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// if the length % 3 is zero, then it has length / 3 values
// but if it doesn't go without rest, the length of the array in the first dimention is + 1
int[][] b = new int[((a.Length % 3 == 0) ? a.Length / 3 : a.Length / 3 + 1)][];

for (int i = 0; i < b.Length; i++)
{
    // the length of the second dimension of the array is the 3 if it goes throught 3
    // but if it has rest, then the length is the rest
    b[i] = new int[(i + 1) * 3 <= a.Length ? 3 : a.Length % 3];
    int[] bTemp = new int[b[i].Length];
    for (int j = 0; j < b[i].Length; j++)
    {
        bTemp[j] = a[i * 3 + j];
    }
    b[i] = bTemp;
}

And this is the result of b

enter image description here

M. Schena
  • 2,039
  • 1
  • 21
  • 29
1

Linq solution:

  int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

  int size = 3;

  // Array of 3 items arrays
  int[][] result = Enumerable
    .Range(0, a.Length / size + (a.Length % size == 0 ? 0 : 1))
    .Select(index => a
       .Skip(index * size)
       .Take(size)
       .ToArray()) 
    .ToArray(); // if you want array of 3-item arrays

Test

  String report = String.Join(Environment.NewLine,
    result.Select(chunk => String.Join(", ", chunk))); 

  // 1, 2, 3
  // 4, 5, 6
  // 7, 8, 9
  // 10
  Console.Write(report);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
-1

The simplest and ugliest way would be

var a = new int[] {1,2,3,4,5,6,7,8,9,10};

var b0 = new int[] {a[0], a[1], a[2]};
var b1 = new int[] {a[3], a[4], a[5]};

otherwise

int[][] b = new int[a.Length][];

        b[0] = a;

by the use of a two dimensional array, you can just select whatever array you want and assign it.

rcarthy
  • 1
  • 2