On 2D array coordinates

This sounds extremely stupid. But it helps me clarify things so I hope it helps for you as well.

when we have a 2D array, the first index, or coordinate is referring to 1D arrays. So take:

[code] 00 01 02 03 10 11 12 13 [/code]

Here it’s showing exactly the indices or subscript. You see that the second index increase first, and then first index.

How does this map to concepts like x-axis, y-axis and column, row?

When 2D array is used to simulate 2D plane, the x coordinate is the second subscript, y the first.

So point(x, y) is arr[y][x]. Because x goes horizontally, and y vertically. In 2D array, the second subscript moves horizontally.

Similarly, for rows and columns, the first subscript decides which row, and second which column.

If you want to fix the row, and iterate through the columns, do arr[fixed][i]. If fix column, iterate through rows under that column, do arr[i][fixed].

Written on March 23, 2016