Find position in an array through the position in the array?

2

If I have an array, eg:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

and I transform this array into an array, eg:

           y[0]  y[1]  y[2]

   x[0] =   1  |  2  |  3
           ---------------
   x[1] =   4  |  5  |  6
           ---------------
   x[2] =   7  |  8  |  9

And I have the position of the initial array , for example the position 5 that in the case is the value -> 6, how do I find the position x, y of the array equivalent to the position of the array, in the case of this example would be: x:1, y:2 .

    
asked by anonymous 03.10.2018 / 18:01

1 answer

2

Friend,

To get the position you simply divide the value by subtracting by 1 and dividing by the number of rows or columns (if it is a perfect matrix) the position x will be the integer value of the division and the rest will be oy Example:

(5-1) / 3 = 1 and the rest is 2 in this case the array will be array [x, y] [1,1]

(6-1) / 3 = 1 and the rest is 2 in this case the array will be array [x, y] [1,2]

    
03.10.2018 / 19:06