Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 8

1.

Introduction to Data Structure


Array as an Abstract Data Type
 Array is finite, ordered set of homogenous elements.

 Array as abstract data type means representing data


objects in memory and implementing operations on
these objects.

 An array is composite or structured data type, which


is made up of simpler data strcuture that exist in a
language.

 On arrays following operations can be performed.


• Create (array, type, size)
• Store (array, index, value)
• Retrive (array, index)
Single dimensional array
 Data-type array-name [size]; e.g. int a[10]
a[0] a[1] a[3] ---------- a[9]

1000 1002 1004 1018


 The lower bound of the array starts at 0 and upper
bound is size -1
 Store operation : array-name[position] = value
 Extract operation : array-name[position]
 Any references to element a[i] is converted to a
pointer notation as,
a[i] = *(a + i)
 The address of an element is calculated as,
Address of a[3] = 1000 + 3 * sizeof (int)
= 1000 + 3 * 2
= 1006
Multi dimensional array
 When each array element is another array, it is
called as multidimensional.
 int a[3][4]
0 1 2 3
0
1
2

 Two dimensional array is logical data structure that


is useful in describing an object that is physically
two dimensional such as a matrix or table. Above is
logical view of data.
 Computer memory is linear, so this stores 2D array
linearly in memory, in the form of,
• Row major Representation
• Column-major Representation
Row major representation :
 Logical view
Columns 0 1 2
R ows
0 1 2 3

1 4 5 6

2 7 8 9

3 10 11 12

 Physical View: Row Major Representation


2 3 4 5 6 7 8 9 10 11 12

R ow 0 R ow 1 R ow 2 R ow 3

 Physical View: Column Major Representation


4 7 10 2 5 8 11 3 6 9 12

C o lu m n 0 C o lu m n 1 C o lu m n 2
Address Calculation
 For a two dimensional array, a[r][c] where r =
number of rows and c = number of columns, the
location of an element a[i][j] in the array can be
calculated as:
Row Major Representation : address of a[i][j]
= Base-address + i*c*element size + j*element size
= Base-address + (i*c+j) * element size
e.g.
address of m[1][2]
= 1000 + (1*3+2) * sizeof (int)
= 1000 + 10
= 1010
Address Calculation
Column Major Representation : address of a[i][j]
= Base-address+ j*r*element-size + i*element–size
= Base-address+(j*r + i)* element–size
e.g. :
address of m[1][2]
= 1000 + (2*4+1) *sizeof (int)
=1000 + 18
=1018
End of Presentation

You might also like