0% found this document useful (0 votes)
18 views5 pages

Calculation of Address of Any Element in 1-D 2-D Array

Uploaded by

vrajpatel.2060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Calculation of Address of Any Element in 1-D 2-D Array

Uploaded by

vrajpatel.2060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Calculating the address of any element in the 1-D array:

A 1-dimensional array (or single-dimension array) is a type of linear array. Accessing its
elements involves a single subscript that can either represent a row or column index.

Example:

To find the address of an element in an array the following formula is used-

Address of A[Index] = B + W * (Index – LB)

Where:

 Index = The index of the element whose address is to be found (not the value of the
element).
 B = Base address of the array.
 W = Storage size of one element in bytes.
 LB = Lower bound of the index (if not specified, assume zero).

Example: Given the base address of an array A[1300 ………… 1900] as 1020 and the size
of each element is 2 bytes in the memory, find the address of A[1700].

Solution:

Given:

 Base address (B) = 1020


 Lower bound (LB) = 1300
 Size of each element (W) = 2 bytes
 Index of element (not value) = 1700

Formula used:
Address of A[Index] = B + W * (Index – LB)
Address of A[1700] = 1020 + 2 * (1700 – 1300)
= 1020 + 2 * (400)
= 1020 + 800
Address of A[1700] = 1820

Calculate the address of any element in the 2-D array:


The 2-dimensional array can be defined as an array of arrays. The 2-Dimensional arrays are
organized as matrices which can be represented as the collection of rows and columns as
array[M][N] where M is the number of rows and N is the number of columns.

Example:

To find the address of any element in a 2-Dimensional array there are two ways-

1. Row Major Order


2. Column Major Order

1. Row Major Order:


Row major ordering assigns successive elements, moving across the rows and then down the
next row, to successive memory locations. In simple language, the elements of an array are
stored in a Row-Wise fashion.
To find the address of the element using row-major order uses the following formula:

Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))

I = Row Subset of an element whose address to be found,


J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in an array(in byte),
LR = Lower Limit of row/start row index of the matrix(If not given assume it as zero),
LC = Lower Limit of column/start column index of the matrix(If not given assume it as
zero),
N = Number of column given in the matrix.
Example: Given an array, arr[1………10][1………15] with base value 100 and the size of
each element is 1 Byte in memory. Find the address of arr[8][6] with the help of row-major
order.

Solution:

Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of column given in the matrix N = Upper Bound – Lower Bound + 1
= 15 – 1 + 1
= 15

Formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))

Solution:
Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1))
= 100 + 1 * ((7) * 15 + (5))
= 100 + 1 * (110)
Address of A[I][J] = 210

2. Column Major Order:

If elements of an array are stored in a column-major fashion means moving across the
column and then to the next column then it’s in column-major order. To find the address of
the element using column-major order use the following formula:

Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))

I = Row Subset of an element whose address to be found,


J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LR = Lower Limit of row/start row index of matrix(If not given assume it as zero),
LC = Lower Limit of column/start column index of matrix(If not given assume it as zero),
M = Number of rows given in the matrix.

Example: Given an array arr[1………10][1………15] with a base value of 100 and the size
of each element is 1 Byte in memory find the address of arr[8][6] with the help of column-
major order.

Solution:

Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of Rows given in the matrix M = Upper Bound – Lower Bound + 1
= 10 – 1 + 1
= 10

Formula: used
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
Address of A[8][6] = 100 + 1 * ((6 – 1) * 10 + (8 – 1))
= 100 + 1 * ((5) * 10 + (7))
= 100 + 1 * (57)
Address of A[I][J] = 157

 Row Major Order V/S Column Major Order

Aspect Row Major Order Column Major Order


Elements are stored column by
Elements are stored row by
Memory Organization column in contiguous
row in contiguous locations.
locations.
For a 2D array A[m][n]:
For the same array: [A[0][0],
Memory Layout Example [A[0][0], A[0][1], …, A[m-1]
A[1][0], …, A[m-1][n-1]]
[n-1]]
Moves through the entire row Moves through the entire
Traversal Direction before progressing to the next column before progressing to
row. the next column.
Efficient for row-wise access, Efficient for column-wise
Access Efficiency less efficient for column-wise access, less efficient for row-
access. wise access.
Commonly used in languages Commonly used in languages
Common Use Cases
like C and C++. like Fortran.
Suitable for row-wise Suitable for column-wise
Applications operations, e.g., image operations, e.g., matrix
processing. multiplication.
Questions:
1) A 2-D array A[4....7, -1....3] requires 2 bytes of storage space for each element. If the array
is stored in row-major from having base address 100, then the address of A[6, 2] will be ___

2) In above question, Calculate A[6,2] for column major order.

3) Consider a 2-dimensional array x with 10 rows and 4 columns, with each element storing a
value equivalent to the product of row number and column number. The array is stored in
row-major format. If the first element x[0][0] occupies the memory location with address
1000 and each element occupies only one memory location, which all locations (in decimal)
will be holding a value of 10?
A) 1018, 1019
B) 1022, 1041
C) 1017, 1036
D) 1000, 1399

4) Let A be a two-dimensional array declared as follows: A: array [1...10] [1...15] of integer;


Assuming that each integer takes one memory location. The array is stored in row- major
order and the first element of the array is stored at location 100. What is the address of the
element A [i][j]?
A) 15j + i + 84
B) 10i + j + 89
C) 15i + j + 84
D) 10j + i + 89

You might also like