 ### One Dimensional Array (1D Array)

Array of an element inside an array say “item[I]” is calculated by using this formula:

AddressOf(item[I]) = B + ( W * ( I + LB) )

Note:

item[I] :  Address of array element is item[I]

B         :  Base Address / First Address of array element. Both are  same.

W          :  Size / Width in byte. For eg. int (2 byte), float(4 byte) and so on.

I           :  Index / Subscript of array elements whose address is to be found.

LB       :  Lower Bound / First element Index value, if not specified assume 0 (Zero)

Example:

Given the base address of an array item[1100 … 2000] as 1024 and size of each element is 2 bytes in ram. Find the address of item.

Sol.:

Given values are:

• Base address (B)   : 1024
• Size (W)                    :  2 bytes
• Lower Bound (LB)  :  1100
• Index (I)                   :   1500

AddressOf( item[I] ) = B + ( W * ( I + LB) )

=  1024 + ( 2 * ( 1500 – 1100) )

=  1024 + ( 2 * 400 )

=  1024 + 800

=  1824

So, item [ 1500 ] address is 1824

### Two Dimensional Array (2D Array)

The elements of a 2-D array in memory, these are stored in contiguous memory locations like 1D Array. 2D Array elements are accessed in two ways:

#### Row Major Order

2D Array can be accessed Row-Wise. The Elements of the 1st row is stored first, then the elements of 2nd row and so on.

Row Major Order is calculated using this formula:

AddressOf( item[R][C] ) = B + W * [ n * ( R – LBR ) + (C – LBC) ]

Note:

R          :  The Row number of the element.

C          :  The Column number of the element.

B          :  Base Address / First Address of array element. Both are  same.

S          :  Size / Width in byte. For eg. int (2 byte), float(4 byte) and so on.

n          :  Total number of columns in the array.

LBR     :  Lower Bound of the Row.

LBC     :  Lower Bound of the Column.

Example:

An Array Item is stored along the row in the memory with each element requiring 8 bytes of storage. If the base address of array Item is 14000, find out the address of Item.

Sol.:

Given values are:

• Item              :  ?
• Base address (B)   :  14000
• Size (W)                    :  8 bytes
• n                               :  10
•                               : 10
• C                               : 7

AddressOf( item[R][C] ) = B + W * [ n * ( R – LBR ) + (C – LBC) ]

Item =  14000 + 8 * [ 10 ( 10 – 0 ) + ( 7 – 0 ) ]

=  14000 + 8 * [ 10 ( 10 ) + 7]

=  14000 + 8 * [ 100 + 7 ]

=  14000 + 8 * 107

=  14000 + 856

=  14856

#### Column Major Order

2D Array can be accessed Column-Wise. The Elements of the 1st column is stored first, then the elements of 2nd column and so on.

Column Major Order is calculated using this formula:

AddressOf( item[R][C] ) = B + W * [ ( R – LBR ) + m * (C – LBC) ]

Note:

R          :  The Row number of the element.

C          :  The Column number of the element.

B          :  Base Address / First Address of array element. Both are  same.

W          :  Size / Width in byte. For eg. int (2 byte), float(4 byte) and so on.

m          :  Total number of Rows in the array

LBC     :  Lower Bound of the Column.

LBR     :  Lower Bound of the Row.

Example:

An array Item is stored in the memory along the column with each of the elements occupying 2 bytes. Find out the memory address of Item, if the element Item is stored at the address 7600.

Sol.:

Given values are:

• Item                 :  7600
• Base address (B)   :  ?
• Size (W)                    :  2 bytes
• m                              :  20
•                               :  2
• C                               :  9

AddressOf( item[R][C] ) = B + W * [ ( R – LBR ) + m * (C – LBC) ]

7600 =  B + 2 * [ ( 2 – 0 ) + 20 * ( 9 – 0 ) ]

7600 =  B + 2 *  ( 2 + 180 )

7600 =  B + 364

B =  7600 – 364

B = 7236

• Item              :  ?
• Base address (B)   :  7236
• Size (S)                    :  2 bytes
• m                              :  20
•                               :  10
• C                               :  5

Item = 7236 + 2 * [ ( 10 – 0 ) + 20 * ( 5 – 0 ) ]

= 7236 + 220

= 7456

Run This Code to Know more about Column Major or Row Major Order

OUTPUT:

Previous article

Next article