How to convert a 2D coordinate into 1D index in C#

1 Answer

0 votes
using System;
 
class Program
{
    static void Main() {
        int[,] array2d = { { 1,   2,   3,   6,  0},
                           {-5,  -4,   0,   7,  9},
                           { 1,   7, 100,  14,  6},
                           { 9,  10,  11,  12, 13} };

        int cols = array2d.GetUpperBound(1) + 1;
        
        int i = 1, j = 3;
         
        int index = i * cols + j;
         
        Console.WriteLine(index);
    }
}
 
 
 
/*
run:
 
8

*/

 



answered Sep 17, 2023 by avibootz

Related questions

1 answer 159 views
1 answer 149 views
1 answer 158 views
1 answer 144 views
1 answer 157 views
1 answer 149 views
1 answer 164 views
...