## TheDeveloperBlog.com

C# Flatten Array

Flatten array. A multidimensional array can be flattened. Its dimensions are reduced to one. This transformation yields a single-dimensional array—one that is simpler and faster. Flattened 2D arrays are also ideal for interop with other languages.

```Flattened array index computation

array[(Y coordinate * width) + X coordinate]

2D array index computation

array[Y coordinate, X coordinate]```

Intro. A 2D array is accessed with a Y and then X position. For rectangular arrays (including all 2D arrays and many jagged arrays) you can use a single array. You multiply the first coordinate by the width, and then add the second coordinate.

Advantages: The advantages of using a flat array are improved performance and interoperability with C++ or other languages.

Note: To use a jagged array, you must have an array of references to arrays. 2D arrays have significant performance penalties.

2D ArraysJagged Arrays

Example. This example creates, assigns to and finally displays a 2D array and its equivalent flattened array. It is contained in a console program with a simple command-line interface. Specify a height of at least 4, and a width of at least 10.

```C# program that uses flattened array

using System;

class Program
{
static void Main()
{
while (true)
{
Console.WriteLine("Enter height: [4+]");

Console.WriteLine("Enter width: [10+]");

//
// A. TWO-DIMENSIONAL ARRAY
//
int[,] twoDimensional = new int[height, width];
// Assign cell 1, 6
twoDimensional[1, 6] = 5;
// Assign cell 3, 9
twoDimensional[3, 9] = 9;
// Assign cell at 2, 3
twoDimensional[2, 3] = 1;

// Display
for (int i = 0; i < height; i++)
{
for (int a = 0; a < width; a++)
{
Console.Write(twoDimensional[i, a]);
}
Console.WriteLine();
}
Console.WriteLine();

//
// B. FLATTENED ARRAY
//
int[] oneDimensional = new int[width * height];
// Assign cell 1, 6
oneDimensional[1 * width + 6] = 5;
// Assign cell 3, 9
oneDimensional[3 * width + 9] = 9;
// Assign cell at 2, 3
oneDimensional[2 * width + 3] = 1;

// Display
for (int i = 0; i < height; i++)
{
for (int a = 0; a < width; a++)
{
Console.Write(oneDimensional[i * width + a]);
}
Console.WriteLine();
}
}
}
}```

The example has some complexity. Part A shows the 2D array with traditional syntax. Part B shows the flattened 1D array. Notice how we use multiplication always when accessing the oneDimensional flat array.

Next: We see some output from the program that demonstrates how the multiplication results in correct output.

```Possible output

Enter height: [4+]
4
Enter width: [10+]
10
0000000000
0000005000
0001000000
0000000009

0000000000
0000005000
0001000000
0000000009

Enter height: [4+]
5
Enter width: [10+]
15
000000000000000
000000500000000
000100000000000
000000000900000
000000000000000

000000000000000
000000500000000
000100000000000
000000000900000
000000000000000

Enter height: [4+]
6
Enter width: [10+]
12
000000000000
000000500000
000100000000
000000000900
000000000000
000000000000

000000000000
000000500000
000100000000
000000000900
000000000000
000000000000```

Summary. We can create, assign values to, and display flattened arrays. This is useful for performance, memory reduction, and interoperability with other systems. In every application I have applied this technique, performance has improved.

And: Flattened arrays are ideal for hashtable bucket implementations as well as certain tree structures.