0% found this document useful (0 votes)
108 views

Data Structures (2D Array)

This lecture discusses two-dimensional arrays. A 2D array can be defined as an array of arrays organized in a matrix with rows and columns. The size of a 2D array is determined by the number of rows and columns. 2D arrays in memory are stored either in row major order, with elements ordered row by row, or column major order, with elements ordered column by column. The location of each element can be calculated from the base address using its row and column indices.

Uploaded by

sajjadimam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Data Structures (2D Array)

This lecture discusses two-dimensional arrays. A 2D array can be defined as an array of arrays organized in a matrix with rows and columns. The size of a 2D array is determined by the number of rows and columns. 2D arrays in memory are stored either in row major order, with elements ordered row by row, or column major order, with elements ordered column by column. The location of each element can be calculated from the base address using its row and column indices.

Uploaded by

sajjadimam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

LECTURE # 07

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

 Movie ratings by multiple reviewers.


• Each row is a different reviewer
• Each column is a different movie
2D ARRAY
 Suppose, A is a two-dimensional m x n array.

 The first dimension of A contains the index set 1, ... m


with lower bound 1 and upper bound m, and the second
dimension of A contains the index set 1, 2, ... n with
lower bound 1 and upper bound n.

 The length of a dimension is the number of integers in its


index set. The pair of lengths m x n is called size of the
array.
SIZE OF ARRAY
 The index set for each dimension is still consists of consecutive
integers from the lower bound to the upper bound of the dimension.
The length of a given dimension can be obtained from the formula,
 Length = UB - LB + 1
 For two-dimensional array we can find the length of the array by the
following procedure,

• First we will find the length of one-dimensional row


array by the formula, L1 =UB1 - LB1 + 1
• Do this again one-dimensional column array,
L2 =UB2 - LB2 + 1
• Length of array (number of elements in two-
dimensional array)
L = L 1x 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)

 The length of row one-dimensional array (5 - 2 +1 = 4)


 The length of column one-dimensional array (1 - (-3)
+1=5)
 Length of given array = 4 x 5 = 20

So, there are 20 elements on the given array.


2D ARRAY REPRESENTATION IN
MEMORY
 Let, A be a two-dimensional array m x n array.
 Although A is pictured as a rectangular array of elements
with m rows and n columns, the array will be represented
in memory by a block m.n sequential memory location.
Specifically, the programming language will store the
array A either,
• Column by Column is called column major
order or
• Row by row, in row major order
COLUMN MAJOR ORDER (CMO)
COLUMN MAJOR ORDER (CMO)
 LOC(A[J,K])=B(A)+w[M(K-L2)+(J-L1)]

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

 Determine LOC (A[0][30] ?

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

 Address of Arr [6][2] = ?

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

 Row Major Order

 Column Major Order

You might also like