3. Index Formula Derivation of 1D Array
3. Index Formula Derivation of 1D Array
N=U–L+1
Question 1: Given A [-1:10], bytes per cell = 4, base address = 2000 find
the address of A [7].
Solution:
Here, i = 7
w= 4
Lower Bound = -1
Upper Bound = 10
Address of A [i] = Base Address +w*(i-Lower Bound)
Address of A [7] = 2000 + 4*(7-(-1)) = 2032
Solution:
Question 2:Given A [1:15], bytes per cell = 3, base address = 1000 find
the address of A [9].
Solution:
Here, i = 9
w= 3
Lower Bound = 1
Upper Bound = 15
Address of A [i] = Base Address +n*(i-Lower Bound)
Address of A [9] = 1000 + 3*(9 - 1) = 1024
Row major
Row major
R=5-2+1=4
C=11+3=1=15
total number of elements= 60
Total memory allocated= 120 bytes
Address of A[3][0] = 236
and A[1][5]= Not exist
1D
Row Major
Address of A[i]= BA + W(i-LB)
2D
Row Major
Address of A[i][j]= BA +w [(i-LB1) X C + (j-LB2)]
3D
Row major
Address of A[i1][i2][i3]= BA + w[(i1-L1)(r2.r3) + (i2-L2)r3 + (i3-L3)]
2D
Row Major
Address of A[i][j]= BA +w [(i-LB1) X C + (j-LB2)]
Column Major
Address of A[i][j]= BA +w [(j-LB2) X R + (i-LB1)]
3D
Row Major
Address of A[i1][i2][i3]= BA + w[(i1-L1)(r2.r3) + (i2-L2)r3 + (i3-L3)]
Column Major
Address of A[i1][i2][i3]= BA + w[(i1-L1)(r2.r3) + (i3-L3)r2 + (i2-L2)]
Dr. Amrita Jyoti 23
Copyright © 2021, ABES Engineering College
Example-3D
Example: Given an array, arr[1:9, -4:1, 5:10] with a base value
of 400 and the size of each element is 2 Bytes in memory find the
address of element arr[5][-1][8] with the help of row-major
order?
Solution: Given:
Lower Bound of dimension 1(L1) = 2
1D
Address of A[i]= BA + W(i-LB)
2D
Address of A[i][j]= BA +w [(i-LB1) X C + (j-LB2)]
3D
Address of A[i1][i2][i3]= BA + w[(i1-L1)(r2.r3) + (i2-L2)r3 + (i3-L3)]
N-D
Address of A[i1][i2]..[in]= BA + w[(in-Ln)+ (in-1-Ln-1)(rn) +(in-2 –Ln-2)(rn.rn-1)+…
…(i1-L1)(r2.r3… rn)