We evaluate address of an element in a 2D array in 2 ways

- Column Major Order
- Row Major Order

## Column Major Order

The Formula is:

`LOC (A [J, K]) = Base (A) + w [M (K-1) + (J-1)]`

So, What is the meaning of this above equation? Explain one by one,

LOC(A[J, K]) = is the location of the element in the Jth row and Kth column.

Base (A) = is the base address of the Array A.

w = is the number of bytes required to store single element of the array A.

M = is the total number of rows in the array.

J = is the row number of the element.

K = is the column number of the element.

So then go through the real time example,

Suppose, A[3,3] array elements are,

```
1 2 3
A = 1=> [10,20,30]
2=> [40,50,60]
3=> [70,80,90]
```

If we represent this array in column wise then

```
Array Indices = Elements = Address (Each element size is 4 bytes because "int")
[1,1] = 10 = 1000
[2,1] = 40 = 1004
[3,1] = 70 = 1008
[1,2] = 20 = 1012
[2,2] = 50 = 1016
[3,2] = 80 = 1020
[1,3] = 30 = 1024
[2,3] = 60 = 1028
[3,3] = 90 = 1032
```

Suppose we have to find the location of A [3, 2]. Then required values are:

```
Base(A) = 1000
w = 4 bytes (Because each elements belongs to integer type assign memory size)
M = 3 (number of rows)
J = 3 (row number of element)
K = 2 (column number of element)
```

Now put these values in the above formula as below:

```
LOC (A [J, K]) = Base(A) + w [M (K-1) + (J-1)]
LOC (A [3, 2]) = 1000 + 4 [3 (2-1) + (3-1)]
= 1000 + 4 [3 * 1 + 2]
= 1000 + 4 [5]
= 1000 + 20
= 1020 (80 is a Element and actual address is 1020).
```

Now,

## Row Major Order

The formula is

`LOC (A [J, K]) = Base (A) + w [N (J-1) + (K-1)]`

So, What is the meaning of this above equation? Explain one by one,

LOC(A[J, K]) = is the location of the element in the Jth row and Kth column.

Base (A) = is the base address of the Array A.

w = is the number of bytes required to store single element of the array A.

N = is the total number of columns in the array.

J = is the row number of the element.

K = is the column number of the element.

So then go through the real time example,

Suppose, A[3,3] array elements are,

```
1 2 3
A = 1=> [10,20,30]
2=> [40,50,60]
3=> [70,80,90]
```

If we represent this array in row wise then

```
Array Indices = Elements = Address (Element Size it's incremented)
[1,1] = 10 = 1000
[1,2] = 20 = 1004
[1,3] = 30 = 1008
[2,1] = 40 = 1012
[2,2] = 50 = 1016
[2,3] = 60 = 1020
[3,1] = 70 = 1024
[3,2] = 80 = 1028
[3,3] = 90 = 1032
```

Suppose we have to find the location of A [3, 2]. Then required values are:

```
Base(A) = 1000
w = 4 bytes (Because each elements belongs to integer type assign memory size)
N = 3 (number of columns)
J = 3 (row number of element)
K = 2 (column number of element)
```

Now put these values in the above formula as below:

```
LOC (A [J, K]) = Base(A) + w [N (J-1) + (K-1)]
LOC (A [3, 2]) = 1000 + 4 [3 (3-1) + (2-1)]
= 1000 + 4 [2 * 1 + 2]
= 1000 + 4 [4]
= 1000 + 16
= 1016 (50 is a Element and actual address is 1016).
```

If you find any issue and interested to rewrite it then contact us.