Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Data Structure

Lecture 4

Address calculation of any element of an Array


Memory allocation of an array:
All the data elements of an array are stored at contiguous locations in the main memory.
The name of the array represents the base address or the address of the first element in the
main memory. Each element of the array is represented by proper indexing. We can define
the indexing of an array in the below ways -

1. 0 (zero-based indexing): The first element of the array will be arr[0].


2. 1 (one-based indexing): The first element of the array will be arr[1].
3. n (n - based indexing): The first element of the array can reside at any random index
number.

In the above image, we have shown the memory allocation of an array arr of size 5. The array follows
a 0-based indexing approach. The base address of the array is 100 bytes. It is the address of arr[0].
Here, the size of the data type used is 4 bytes; therefore, each element will take 4 bytes in the
memory.

Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 4

Solution:

K=-1, BA(A)=999, Lower Bound=-10, W=2;

So, LOC(A[-1])=999+2(-1-(-10))=1017

Solution:

LOC(AUTO[1965])=200+4(1965-1932)=200+4*33=200+132=332

Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 4

Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 4

In row major ordering, all the rows of the 2D array are stored into the memory
contiguously. Considering the array shown in the above image, its memory allocation
according to row major order is shown as follows.

first, the 1st row of the array is stored into the memory completely, then the 2 nd row
of the array is stored into the memory completely and so on till the last row.

Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 4

Solution: Here,

BA(A)=200, W=4, N=4, J=12, K=3

LOC(A[12,3])=200+4*[4(12-1)+(3-1)]=200+4[44+2]=200+4*46=384

2. Column Major ordering


According to the column major ordering, all the columns of the 2D array are stored
into the memory contiguously. The memory allocation of the array which is shown in
in the above image is given as follows.

First, the 1st column of the array is stored into the memory completely, then the
2nd row of the array is stored into the memory completely and so on till the last
column of the array.

Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 4

Solution: Here,

BA(A)=200, W=4, M=25, J=12, K=3

LOC(A[12,3])=200+4*[(12-1)+25(3-1)]=200+4[11+50]=200+4*61=200+244=444

Multi Dimensional Array Address Calculation


General Multidimensional Arrays

General multidimensional arrays are defined analogously. More specifically. an n-dimensional m 1 x


m2... x mn, array B is a collection of m1, m2, --------- mn data elements in which each element is
specified by a list of n integers—such as K1, K2, K3, --------, Kn—called subscripts. with the property
that

1≤ K1≤m1, 1≤ K2≤m2, ------------------- 1≤ Kn≤mn

The element of B with subscripts K1, K2, K3, --------, Kn will be denoted by
BK1,K2,--------Kn or B[K1, K2, K3, --------, Kn]

Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 4

Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 4

Solution:
Li=UB-LB+1
L1=8-2+1=7
L2=1-(-4)+1=6
L3=10-6+1=5

NOE=L1*L2*L3=7*6*5=210

and K1=5, K2=-1, K3=8; w=4

E1=K1-LB=5-2=3
E2=K2-LB=-1-(-4)=3
E3=K3-LB=8-6=2

Address of A[5,-1,8] = BA+w*((E3L2+E2)L1+E1) = 200+4*[(2*6+3)*7+3] = 200+4[108] = 632

Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.
Data Structure
Lecture 4

Solution:
Li=UB-LB+1
L1=8-2+1=7
L2=1-(-4)+1=6
L3=10-6+1=5

NOE=L1*L2*L3=7*6*5=210

and K1=5, K2=-1, K3=8; w=4

E1=K1-LB=5-2=3
E2=K2-LB=-1-(-4)=3
E3=K3-LB=8-6=2

Address of A[5,-1,8] = BA+w*((E3L2+E2)L1+E1) = BA + w*[((E)L1+E2)L3+E3]

References:
1. DATA STRUCTURES WITH C by Seymour Lipschutz, 2010.

2. https://www.javatpoint.com/data-structure-2d-array

3. https://www.youtube.com/watch?v=NrhuLHp2vRw

4. https://www.youtube.com/watch?v=djidEjstwRM

5. https://www.youtube.com/watch?v=r-QlbzVJLT4&t=200s

Prepared By Abdullah Rajib, Assistant Professor, RTM Al-Kabir Technical University, Sylhet,
Bangladesh.

You might also like