Accessing multidimensional array elements

Hi, I have a simple question (might need some math for it, but whatever) about accessing elements of an array as if it was a 2 dimensional array.

Let's say I have an array :

float array[16];

And I want to access elements by providing two values, the row and column :

float getElement(int row, int column) {...}

So the array should be portrayed like this :

0, 1, 2, 3
4, 5, 6, 7
8, 9, 10,11
12,13, 14,15

And if I do something like :

print(getElement(1, 2)); //<-- this should give me : 6

How will this function be implemented?
Last edited on
One way is to pass in the width as a 3rd parameter, or you could create a wrapper for the array in an object and make that store the width.

the formula is like this: y * width + x
1
2
3
float getElement(int row, int column) {
    return array[4*row+column];
}
thank you so much for the quick reply :) That solved it :)
Topic archived. No new replies allowed.