Once upon a time, there was a language that didn’t have 2 dimensional arrays. This greatly upset the programmer. Fortunately, the programmer was swift in the art of arithmetic and overcame this obstacle easily.

Lets say for example, we only have access to a 1 dimensional array data structure and it has 50 elements in it.

1 |
int data[50]; |

Lets break this array up into 10 rows and 5 columns to make a grid. So how do we get access to a position say data[4][3]?

There are two things I’m going to teach you now:

1) Retrieve the column & row numbers from a linear index counter.

2) Retrieve the index from a row & column numbers.

Before I continue, i want you to notice that the row and column numbers are zero based. So they start at zero, not one.

Lets do #1 first:

1 2 3 4 5 6 |
unsigned int index = 6; unsigned int numRows = 10; unsigned int numCols = 5; int row = index / numRows; int col = index % numCols; |