Celeb Glow
updates | April 21, 2026

How do I convert an index for a one-dimensional array into x and y?

$\begingroup$

Given a flat array of values as such, representing a Sudoku board:

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

As well as knowing the amount of rows & columns, such as 9 x 9

How do I convert an index for any point on the array into an x and y coordinate as if it were a 9x9 grid?

For example, when provided with rows & columns 9x9 and index 10 should yield x: 2, y: 2 and 80 should yield x: 9, y: 9

$\endgroup$ 2

2 Answers

$\begingroup$

You use division and modulo. Given index 10, you have $x=10/9$ and $y=10\%9$ (using programming syntax). This is using an index that starts at 0. If you want to start at one, then just add one to each of these.

$\endgroup$ 3 $\begingroup$

Use double arrays. In programming languages this is often represented by: a[][]

Then you can define a[1][1] etc.

$\endgroup$ 2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy