I have a two-dimensional array of [m, n] elements. How can I find out the sequence number of an element in an array? Indexing from scratch.

Example: int [,] array = new int [3,2]; array [0,0] - zero element array [0,1] - first element array [1,0] - second element array [1,1] - third element And so on.

  • In general, some strange logic seems to me, 1.1 is the third element. and 0.3 - what and 3.1 what and 2.2 what will be? - user2455111

2 answers 2

array[i,j] порядковый номер i*n+j 
  • maybe i + j-1? - user2455111
  • 2
    @ user2455111 um Check your theory for an array [4,4]. The last element of a [3,3] will be the 15th, and according to your logic the 5th. - Trymount
  • @ user2455111 and the element a [0,0] will be -1) - Trymount
  • Yes, yes, I already realized that this is the same matrix, it won't work out there - user2455111

array[m,n] , for example array(3,4) (3 lines, 4 columns):

The sequence number of the array element = (row-1) * n + col

if you start from zero make another -1