0

I have multidimensional 3*3 for example user input is 4 that calculate need to be array[1,0] user input is 5 that calculate need to be array[1,1] user input is 2 that calculate need to be array[0,1] user input is 3 that calculate need to be array[0,2]

how can i accomplish this in the best way in c# thanks

1 Answer 1

1

If you have n rows and n columns, then the k-th element in the order that you described is given by:

row = (k - 1) / n
column = (k - 1) % n

If you have m columns, then:

row = (k - 1) / m
column = (k - 1) % m

As you can see, it's only the number of columns that matters (unless you also need to detect potential index out of bounds errors, then you also need the number of rows).

Sign up to request clarification or add additional context in comments.

1 Comment

I'm just impressed that you understood the question! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.