Given a 2D square array, how do I get the row and ...
# mathematics
s
Given a 2D square array, how do I get the row and column given a position? Can't quite get the modulos right. E.g. in a 5x5 array, how do I find the 13th element?
i
Copy code
var idx = 0
val array = Array(5) { Array(5) {++idx} }
val n = 13
println(array[(n - 1) / array[0].size][(n - 1) % array[0].size])
a
In linear computations you usually only have to check that border cases a correct.