cmultidimensional-arrayrow-major-ordercolumn-major-order

Representation of Column major order vs Row major order


Consider a 2D array "Array" that contains values : {1,2,3,4,5,6,7,8,9}. My major issue with the understanding of both orders is that if Row Major Order is to be represented as Array[i,j] (while i is row and j is column),

    [i0,j0][i0,j1][i0,j2]
    [i1,j0][i1,j1][i1,j2]
    [i2,j0][i2,j1][i2,j2]

So that if you are asked with a question such as "find the address of element Array[1,2] in Array[2][2]", you'd know that the number of rows come before the number of columns and it would be easy to put them in the formula:

    Base(Address) + w(dataSize){N(i - Row_lowerBound)+(j - Col_lowerBound)}
    While 'N' is the number of columns

does it mean that the column order can be represented as Array[j,i] which would mean that the Column number appears earlier than the row number. So there is no way of knowing where the i,j,Row_lowerbound and Col_lowerbound values of Array[j][i] (e.g Array[3][4]) are to be put in the formula.

for e.g if the question appears as: "Find the address of Array[1][2] in the array Array[3][4]" How would you know if 3 is the number of colmns or 4? How would you know whether the 'i' is 3 or 4?


Solution

  • You can't know. There is no relation between how the computer represents the data and how you interpret it. Since you are "free" to interpret it the way you like, then it's your responsability to know which index is for rows and which one for columns.

    Usually you can, write the specification in comments in the code itself or write documentation for functions using the structure. Also, you could write functions to access the array that enforce the order of indexes. But then, it's still not immediately unambiguous but has the benefit that you can check for out of bound access.