I realize this problem and solutions are all over StackOverflow like here however I'm still unable to make this work.

Most of the examples say that I just need to multiply the row by the width and add the column meaning the location (4, 3) in a 4x4 square grid would become (3 * 4 + 4) or 16. So far so good.

The examples say to get back the coordinates, I should divide the index by the number of rows for x and get the modulo of the index for y. For the example above, that should be...

```
int x = 16 / 4;
int y = 16 % 4;
```

But this works for some values, and not others. In this case, when I get back the coordinates after converting to an index, I get (4,0). This makes sense since 4 goes into 16 evenly so I must be missing something basic.

Here's some test Java code that I've created for trying to figure this out. I should mention that I'm indexing started at 1 so the first square in the upper left corner is 1,1 and the last square would be 4,4.

```
public class Test {
int size;
public Test(int size) {
this.size = size;
}
public int toIndex(int x, int y) {
return x * this.size + y;
}
public int[] toCoordinates(int index) {
int coordinates[] = new int[2];
coordinates[0] = index / this.size;
coordinates[1] = index % this.size;
return coordinates;
}
public static void main(String[] args) {
int testSize = 4;
Test test = new Test(testSize);
for (int i = 1; i <= testSize; i++) {
for (int j = 1; j <= testSize; j++) {
int index = test.toIndex(i, j);
int coordinates[] = test.toCoordinates(index);
System.out.println(index + " == " + coordinates[0] + "," + coordinates[1]);
}
}
}
}
```

The output of the current code is

```
5 == 1,1
6 == 1,2
7 == 1,3
8 == 2,0
9 == 2,1
10 == 2,2
11 == 2,3
12 == 3,0
13 == 3,1
14 == 3,2
15 == 3,3
16 == 4,0
17 == 4,1
18 == 4,2
19 == 4,3
20 == 5,0
```