Math trickery for 1D & 2D arrays
August 14th, 2011
No comments
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.
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.