0% found this document useful (0 votes)
114 views17 pages

Address Calculation in Arrays

The document explains address calculation for linear data structures, specifically focusing on single and double dimensional arrays. It details how to compute the address of elements in these arrays using formulas that incorporate base address, word width, and indices. Additionally, it covers both row-major and column-major orientations for double dimensional arrays, providing examples for clarity.

Uploaded by

ruhan.anup.shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views17 pages

Address Calculation in Arrays

The document explains address calculation for linear data structures, specifically focusing on single and double dimensional arrays. It details how to compute the address of elements in these arrays using formulas that incorporate base address, word width, and indices. Additionally, it covers both row-major and column-major orientations for double dimensional arrays, providing examples for clarity.

Uploaded by

ruhan.anup.shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Address Calculation

Linear data structure is the logical structure created in


memory to contain data linearly or sequentially. Such
structures are arrays, stacks and queues. The number of
bytes occupied in the memory by an element of array
depends upon the type of data it contains.
Hence, to manage the memory efficiently and accessing
the data properly from the array, it is must to know the
physical address of an element that is stored in the array.
This I why, address calculation is essential in an array.
Address Calculation in Single
Dimensional Array
 Let the array be Arr. The address of an element Arr[I] can be
calculated as:
 Address B+W(I-L)
 Where,
 B = Base address
 W = Word width(Number of bytes an element contains in the memory)
 I = Index or cell number of the element whose address is to be calculated.
 L = Lower boundary i.e. the lowest index of the array.
Number of bytes contained by an
element in the memory :
 char type : 2 bytes
 int type : 4 bytes
 float type : 4 bytes
 long type : 8 bytes
 double type: 8 bytes
Lowest call number or array index is taken to be 0 by default
unless mentioned otherwise. If the cell rage is mentioned as Arr[-
4….10], the lowest subscript is to be taken as -4 for address
calculation.
A single dimensional array Arr contains 10 integer
numbers. If the base address of the array is 1000, find
the address of 6th and 8th elements of the array.
 Solution : 10 20 30 40 50 60 70 80 90 100
 B=1000 1000 1004 1008 1012 1016 1020 1024 1028 1032 1036
 I=6
 W = 4 (Word width of Integer number is 4)
 L = 0 (Lowest cell number is 0 by default)
 Address of 6th element = B+W(I-L)
 =1000+4(6-0)
 =1000+24
 =1024

 Address of 8th element = B+W(I-L)


 =1000+4(8-0)
 =1000+32
 =1032
Given the base address of an array B[1300….1900] as 1020 and
size of each element is 2 bytes in the memory. Find the address
of B[1700]
 The given values are B=1020, LB=1300, W=2, I=1700
 Address of A[I]=B+W(I-LB)
 =1020+2(1700-1300)
 =1020+2(400)
 =1020+800
 =1820
A[-25……75]
B=1000, W=10Bytes, A[60]=? (LB=-25 & UB=75)

 A[I]=B+W(I-LB)
=A[60]=1000+10(60-(-25))
 =1000+10(85)
 =1850
 (If want to see number of elements in an array :
 N=UB-LB+1
 N=75-(-25)+1
 =101
Address Calculation in Double Dimension
Array
 In double dimension array, the orientation of the
elements can be row-wise or column-wise. The
address of an element can be different as per
orientation of the elements in double dimensional
array. Hence, there are separate systems for
calculating addresses in row major form and
column major form orientation of the elements in
the matrix.
Row Major Orientation

 Address calculation of an element Arr[I][J] can be:


 Address of element Arr[I][J]=B+W[N(I-R0)+(J-C0)]
 B= Base address
 W=Word width
 I =Row subscript of the element
 J=Column subscript of the element
 R0=Lower range of the row (Lowest row number)
 C0=Lower range of the column(Lowest column number)
 N=Number of columns.
A DOUBLE DIMENSIONAL ARRAY Arr[4][10] contains character. If
Arr[0][0] is stored at the memory location 200, find the address
of Arr[3][7]. If matrix is: Row major form
 B= Base address
 Solution : B=200
 W=Word width
W=2
I=3  I =Row subscript of the element

J=7  J=Column subscript of the element


R0=0  R0=Lower range of the row (Lowest row
C0=0 number)

N=10
 C0=Lower range of the column(Lowest
column number)
M=4

 N=Number of columns.
Address of element Arr[I][J]=B+W[N(I-R0)+(J-C0)]
= 200+2[10(3-0)+(7-0)]
= 200+2(30+7)
= 200+2*37
=274
Column Major Orientation
 Address calculation of an element Arr[I][J] can be:
 Address of element Arr[I][J]=B+W[(I-R0)+M(J-C0)]
 B= Base address
 W=Word width
 I =Row subscript of the element
 J=Column subscript of the element
 R0=Lower range of the row (Lowest row number)
 C0=Lower range of the column(Lowest column number)
 N=Number of rows.
A DOUBLE DIMENSIONAL ARRAY Arr[4][10] contains character. If
Arr[0][0] is stored at the memory location 200, find the address
of Arr[3][7]. If matrix is: Column major form
 Solution : B=200
W=2
I=3
J=7
R0=0
C0=0
N=10
M=4
 Address of element Arr[I][J]=B+W[(I-R0)+M(J-C0)]
= 200+2[(3-0)+4(7-0)]
= 200+2(3+28)
= 200+2*31
= 200+62
=262
 System uses number of rows and columns by default
as 0 unless mentioned. Sometimes the dimension of
a matrix may be specified in the form Arr[LR….UR,
LC…UC]. In such situation, the number of rows and
the number of columns can be calculated as follows:
 Number of rows(N) : (UR-LR)+1
 Number of columns(M): (UC-LC)+1
 Where,
 LR = Lowest row number
 UR = Highest row number
 LC = Lowest column number
 UC = Highest column number
A Double dimensional array Arr[-10..12, -15…20] contains elements in
such a way that each element occupies 4 byte memory space. If the base
address of Array is 2000, find the address of the array element arr[-6][8].
Perform the calculation assuming that the elements are : Row major

oriented
Solution B=2000
I = -6
J=8
R0 = -10
C0 = -15
W=4
Number of rows(M)=[12-(-10)]+1=23
Number of columns (N) = [20-(-15)]+1=36
Address of element Arr[I][J]=B+W[N(I-R0)+(J-C0)]
=2000+4[36(-6-(-10))+8-(-15))]
=2000+4[36(-6+10)+(8+15)]
=2000+4[(36*4)+23]
=2000+4*(144+23)
=2000+4*167
=2000+668
A Double dimensional array Arr[-10..12, -15…20] contains elements in
such a way that each element occupies 4 byte memory space. If the base
address of Array is 2000, find the address of the array element arr[-6][8].
Perform the calculation assuming that the elements are : Column major

oriented
Solution B=2000
I = -6
J=8
R0 = -10
C0 = -15
W=4
Number of rows(M)=[12-(-10)]+1=23
Number of columns (N) = [20-(-15)]+1=36
Address of element Arr[I][J]=B+W[(I-R0)+M(J-C0)]
=2000+4[(-6-(-10))+23(8-(-15))]
=2000+4[(-6+10)+23(8+15)]
=2000+4[4+23*23]
=2000+4*(4+529)
=2000+4*533
=2000+2132
A double dimensional array Arr[4][4] contains floating type
elements. If Arr[2][2] is stored at memory location 2000, find
the base address of the array. Assume that the array is row
major oriented.
 Solution B=?
I=2
J=2
R0=0
C0=0
Number of columns (N)=4
 Address of element Arr[I][J]=B+W[N(I-R0)+(J-C0)]

Address of element Arr[2][2]=B+4[4(2-0)+(2-0)]


or, 2000=B+4[4*2+2]
or, 2000=B+4*10
or, 2000=B+40
Hence, Base address (B)=2000-40=1960
A double dimensional array Arr[4][4] contains integer
numbers. Find the address of Arr[3][2], if the array is
column major oriented. The base address of the array is
1000.
 Solution
B = 1000
W=4
I=3
J=2
Number of rows (M) = 4
Address of element Arr[I][J]=B+W[(I-R0)+M(J-C0)]

=1000+[4(3-0)+4(2-0)]
=1000+4*(3+8)
=1000+4*11
=1000+44
=1044

You might also like