Two Dimensional Array

597 views 14 slides Dec 30, 2020
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

Two Dimensional Array,memory representation,address calculation


Slide Content

MULTI DIMENSIONAL
ARRAY
DincyR Arikkat
Assistant Professor
1

2
Two-Dimensional Arrays
Thearrayswehavediscussedsofarare
knownasone-dimensionalarraysbecause
thedataareorganizedlinearlyinonlyone
direction.
Manyapplicationsrequirethatdatabe
storedinmorethanonedimension.
Onecommonexampleisatable,whichis
anarraythatconsistsofrowsandcolumns.
Atwo-dimensionalarrayconsistsofa
certainnumberofrowsandcolumns.

3
FIGURE Two-dimensional Array

4
FIGURE Array Of Arrays

5
FIGURE Memory Layout

6

MEMORY REPRESENTATION
OF MATRIX
Row-Major Order
Column-Major Order
7

ROW MAJOR
In this method the elements are stored row wise.
i.e. n elements of first row are stored in first n locations, n elements of
second row are stored in next n locations and so on.
8
A[0][0]A[0][1]A[0][2]A[0][3]
A[1][0]A[1][1]A[1][2]A[1][3]
A[2][0]A[2][1]A[2][2]A[2][3]
A[0][0]200
A[0][1]201
A[0][2]202
A[0][3]203
A[1][0]204
A[1][1]205
A[1][2]206
A[1][3]207
A[2][0]208
A[2][1]209
A[2][2]210
A[2][3]211
ElementAddress

COLUMN MAJOR
In this method the elements are stored column wise.
i.e. m elements of first column are stored in first m locations, m elements of second
column are stored in next m locations and so on.
9
A[0][0]A[0][1]A[0][2]A[0][3]
A[1][0]A[1][1]A[1][2]A[1][3]
A[2][0]A[2][1]A[2][2]A[2][3]
A[0][0]200
A[1][0]201
A[2][0]202
A[0][1]203
A[1][1]204
A[2][1]205
A[0][2]206
A[1][2]207
A[2][2]208
A[0][3]209
A[1][3]210
A[2][3]211
ElementAddress

ADDRESS CALCULATION
Row Major System:
Address of A [ I ][ J ] = B.A + [( I –Lr )* N + ( J –Lc ) ]*Size
Column Major System:
Address of A [ I ][ J ] = B.A + [( I –Lr ) + ( J –Lc )*M]*Size
10

ADDRESS CALCULATION
B.A= Base address
I= Row subscript of element whose address is to be found
J= Column subscript of element whose address is to be found
Size= Storage Size of one element stored in the array (in byte)
Lr= Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Lc= Lower limit of column/start column index of matrix, if not given assume 0
(zero)
M= Number of row of the given matrix
N= Number of column of the given matrix
Usually number of rows and column of a matrix are given like A[10][15],
A[5][2] but if it is given as A[Lr......Ur][Lc.......Uc]
So in this case number of rows and columns will be calculated as
rows(M)= (Ur-Lr)+1
column(N)=(Uc-Lc)+1
11

12
Multidimensional Arrays
•Multidimensionalarrayscanhavethree,
four,ormoredimensions.
•TheClanguageconsidersthethree-
dimensionalarraytobeanarrayoftwo-
dimensionalarrays.

13
FIGURE A Three-dimensional Array (3 x 5 x 4)

Thank You
14