Data Structures (2D Array)
Data Structures (2D Array)
INTRODUCTION TO 2D ARRAY
Instructor:
Ms.Dur-e-Shawar Agha
ROAD MAP
Introductionto 2D Array
Size of 2D Array
2D Array Memory Representation
Row Major Order
Column Major Order
2D ARRAY
The two-dimensional array can be defined as an array of
arrays. The 2D array is organized as matrices which can
be represented as the collection of rows and columns.
There is a standard way of drawing a two-dimensional m
x n array X where, the elements of A form a rectangular
array with m rows and n columns and where, the
elements X[J.K] appears in row J and column K.
EXAMPLE OF 2D ARRAY
Lab book of multiple readings over several days
Periodic table
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
CMO EXAMPLE
Consider A [ -20 _ 20 , 10 – 35]
One byte of storage element
B=500
Solution
M =UB – LB + 1
M = 20 – ( -20) + 1
M = 20 + 20 + 1
M = 41
L1 = -20
L2 = 10
CMO EXAMPLE
LOC (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
ROW MAJOR ORDER (RMO)
ROW MAJOR ORDER (RMO)
LOC (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
Solution
B=100
W=2
J=6
K=2
L1=5
L2= -1
RMO EXAMPLE
N= UB – LB + 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
LOC(Arr[6][2]) = 126
SUMMARY
Introduction to 2D array
Size of 2D array