Column Major Order Representation of Array
Column Major Order is a method of representing multi dimension array in sequential memory.
In this method elements of an array are arranged sequentially column by column. Thus elements of first column occupies first set of memory locations reserved for the array, elements of second column occupies the next set of memory and so on.
Consider a Two Dimensional Array consist of N rows and M columns. It can be stored sequentially in memory column by column as shown below:
Consider following example in which a two dimensional array consist of two rows and four columns is stored sequentially in Column Major Order as:
The Location of element A[i, j] can be obtained by evaluating expression:
LOC (A [i, j]) = Base_Address + W [N (j) + (i)]
Base_Address is the address of first element in the array.
W is the word size. It means number of bytes occupied by each element.
N is number of rows in array.
M is number of columns in array.
Suppose we want to calculate the address of element A [1, 2].
It can be calculated as follow:
Base_Address = 2000, W= 2, M=4, N=2, i=1, j=2
|LOC (A [i, j])||=||Base_Address + W [N (j) + (i)]|
|LOC (A[1, 2])||=||2000 + 2 *[2*(2) + 1]|
|=||2000 + 2 * [4 + 1]|
|=||2000 + 2 * 5|
|=||2000 + 10|