Data Structures (2D Array)
Data Structures (2D Array)
I N T R O D U C T I O N TO
2D A R R AY
Instructor:
Ms. Falak Saleem
ROAD M AP
Introduction to 2D Array
Size of 2D Array
Periodic table
• array, L 2 =UB
Do this again 2 - LB 2 + 1
one-dimensional column
• Length of array (number of
elements in two-dimensional array)
L = L1 x L2
EXAMPLE
Find total number of elements in N(2:5, 3:1)
Solution
M =5–2+1
M=4
N = 1 – ( -3 ) + 1
N =1+3+1
N=5
Length of total number of elements = M x N
Length of total number of elements = 4 x 5
Length of total number of elements = 20
EXAMPLE
Given array int A(2 : 5, - 3 : 1)
Where,
M = Number of rows
B = Base address of array
W = Size of element (in bytes)
J = The desired row which I have to find
K = The desired column which I have to
find
L1 = Lower bound of row
L2 = Lower Bound of column
C M O EXAMPLE
Consider A [ -20 _ 20 , 10 – 35]
One byte of storage element
B=500
Determine L O C (A[0][30] ?
Solution
M =UB – LB + 1
M = 20 – ( -20) + 1
M = 20 + 20 + 1
M = 41
L1 = -20
L2 = 10
C M O EXAMPLE
L O C (A[J][K]) = B(A) + W[ M(K – L2) + ( J – L1)]
= 500 + 1[ 41( 30 – 10) + (0 + 20)]
= 500 + 1[ 41(20) + (20)]
= 500 + 1 [ 820 + 20]
= 500 + 840
LOC(A[0][30]) = 1340
R OW M A J O R O R D E R (RMO)
R OW M A J O R O R D E R (RMO)
L O C ( A [ J , K]) = B(A) + w [ N ( J – L1) + (K – L2)]
Where,
N = Number of columns
B = Base address of array
W = Size of element (in bytes)
J = The desired row which I have to find
K = The desired column which I have to find
L1 = Lower bound of row
L2 = Lower Bound of column
RMO EXAMPLE
Consider Arr [ 4 – 7 , 1 – 3]
2 bytes of storage for each element
Base = 100
Address of Arr [6][2] = ?
Solution
B=100
W=2
J=6
K=2
L1=5
L2= -1
RMO EXAMPLE
N= U B – L B + 1
N = 3 – (-1) + 1
N =3+1+1
N=5
LOC(Arr[6,2]) = 100 + 2 [ 5 (6 – 4) + ( 2 +
1)]
= 100 + 2 [ 5
(2) + 3 ]
= 100 + 2 [ 10
+ 3]
= 100 + 2 [13]
= 100 + 26
S U M M A RY
Introduction to 2D array
Size of 2D array