Array Class
Array Class
A[ i ] = B.A.+(i ̶ l) × c
where,
i = index
B.A.= Base Address (OR Offset)
l = lower bound of an array
c = size of each element in bytes
(Scale Factor)
Example
A[ -2 : 3 ] (array of int with BA=2001)
Find Address of A[2].
No of elements: (u – l + 1) = (3 - ( - 2 )
+1) = 6
l = -2, i =2
u = 3,
c = 4, BA= 2001
A[i] = B.A.+(i - l) × c
= 2001+(2 –(-2)) × 2
= 2001+(4 × 2)
= 2009
Two Dimensional Array
Notation: A[ lr : ur , lc : uc ]
where,
A=Array Name
lr =lower bound for row, ur =upper bound
for row
lc =lower bound for col, uc =upper bound
for col
Calculate elements
No of rows: r = (ur – lr + 1)
No of columns: c = (uc – lc + 1)
No of elements: r×c = (ur – lr + 1)×(uc – lc
+ 1)
Example (Row-Major Order)
If D[-13:1, 4:9] is an array of float find the
address of D[-2, 8]. Address of D[-13,4] is
3000.
Here, lr =-13, ur =1, BA=3000, c=4(
lc =4, uc =9, i=-2, j=8
Here
Here Array of float A [-100:52, 60:89]
Example
Given a two dimensional array Zl (3:l0,
10:20) stored in row-major order with base
address of 200 and size of each element of
4 bytes, find address of element Z1(5, 15).
Given 2D array M[-12:15, 2:22] with base
address 1001 and type of the element is
float. Find the address of element M[-7,20]
using row major and column major order.
Gate 2004
Two matrices M1 and M2 are to be stored in arrays A and B
respectively. Each array can be stored either in row-major or
column-major order in contiguous memory locations. The time
complexity of an algorithm to compute M1 × M2 will be
(A) best if A is in row-major, and B is in column- major order
(B) best if both are in row-major order
(C) best if both are in column-major order
(D) independent of the storage scheme
Answer: (D)
1. Row major:
2. Column major:
Find the address of an element in
3-D
Stored in Row major:
No of elements in all
previous rows=
( j – lr)(uc – lc +1)
No of elements in
current row=(k – lc)
For Row Major & Column Major
Example
Row Major:
A[i,j,k] =B.A.+[(i – lP)(ur – lr +1)(uc – lc +1)+( j – lr)(uc – lc +1)+(k – lc)]×c
= 140+[(0-(-2))((-1)-(-4)+1)(3-1+1)+((-2)-(-4))(3-
1+1)+(2-1)]×2
= 140+[(2)(4)(3)+(2)(3)+1]×2
= 140+[31]×2 = 202
Column
A[i,j,k] =B.A.+[(iMajor:
– lP)(ur – lr +1)(uc – lc +1)+ (k – lc)(ur – lr +1)+( j – lr)]×c
= 140+[(0-(-2))((-1)-(-4)+1)(3-1+1)+(2-1))((-1)-(-4)+1)+
((-2)-(-4)]×2
= 140+[(2)(4)(3)+(1)(4)+2] ×2
= 140+[30] ×2 = 200