https://kotlinlang.org logo
#mathematics
Title
# mathematics
s

Sam Stone

01/01/2023, 10:23 PM
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

Iaroslav Postovalov

01/02/2023, 7:34 AM
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

altavir

01/03/2023, 11:09 AM
In linear computations you usually only have to check that border cases a correct.
2 Views