C# : 2D

[".0s4*0|collections;datetime-format;2d",["F@eBFNO","MAEEBEFCJC","OYFOCCBEOSTUUUUTTUUUUPGHGDGDGDGDGHGDGDGDGDOCPWCSTTUUUUTTUUUU",".sywrr.",".NET","Array","Dictionary","List","String","2D","Async","Console","DataTable","Dates","DateTime","Enum","File","For","Foreach","Format","IEnumerable","If","IndexOf","Lambda","LINQ","Optimization","Parse","Path","Process","Property","Random","Regex","Replace","Sort","Split","Static","Substring","Switch","Tuple","While","Flatten Array."," A multidimensional array can be flattened. Its dimensions are reduced to one. This transformation yields a single-dimensional array\u2014one that is simpler and faster. Flattened 2D arrays are also ideal for interop with other languages. ","Flattened array index computation","\n\narray[(Y coordinate * width) + X coordinate]\n\n","2D array index computation","\n\narray[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 Arrays ","2d","Jagged Arrays ","jagged-array","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. ","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","C# program that uses flattened array","\n\nusing System;\n\nclass Program\n{\n static void Main()\n {\n while (true)\n {\n Console.WriteLine(\"Enter height: [4+]\");\n int height = int.Parse(Console.ReadLine());\n\n Console.WriteLine(\"Enter width: [10+]\");\n int width = int.Parse(Console.ReadLine());","\n\n //\n // A. TWO-DIMENSIONAL ARRAY\n //\n ","int[,] twoDimensional = new int[height, width];","\n // Assign cell 1, 6\n ","twoDimensional[1, 6] = 5;","\n // Assign cell 3, 9\n ","twoDimensional[3, 9] = 9;","\n // Assign cell at 2, 3\n ","twoDimensional[2, 3] = 1;","\n\n // Display\n ","for (int i = 0; i < height; i++)\n {\n for (int a = 0; a < width; a++)\n {\n Console.Write(twoDimensional[i, a]);\n }\n Console.WriteLine();\n }\n Console.WriteLine();","\n\n //\n // B. FLATTENED ARRAY\n //\n ","int[] oneDimensional = new int[width * height];","\n // Assign cell 1, 6\n ","oneDimensional[1 * width + 6] = 5;","\n // Assign cell 3, 9\n ","oneDimensional[3 * width + 9] = 9;","\n // Assign cell at 2, 3\n ","oneDimensional[2 * width + 3] = 1;","\n\n // Display\n ","for (int i = 0; i < height; i++)\n {\n for (int a = 0; a < width; a++)\n {\n Console.Write(oneDimensional[i * width + a]);\n }\n Console.WriteLine();\n }\n }\n }\n}","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","\n\nEnter height: [4+]\n4\nEnter width: [10+]\n10\n0000000000\n0000005000\n0001000000\n0000000009\n\n0000000000\n0000005000\n0001000000\n0000000009\n\nEnter height: [4+]\n5\nEnter width: [10+]\n15\n000000000000000\n000000500000000\n000100000000000\n000000000900000\n000000000000000\n\n000000000000000\n000000500000000\n000100000000000\n000000000900000\n000000000000000\n\nEnter height: [4+]\n6\nEnter width: [10+]\n12\n000000000000\n000000500000\n000100000000\n000000000900\n000000000000\n000000000000\n\n000000000000\n000000500000\n000100000000\n000000000900\n000000000000\n000000000000","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.","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto"],"url(data:image/png;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