6
ABSTRACT DATA TYPE ARRAY Abstract Data Type It can be defined as a collection of data items together with the operations on the data. The word “abstract” refers to the fact that the data and the basic operations defined on it are being studied independently of how they are implemented. It involves what can be done with the data, not how has to be done. For ex, in the below figure the user would be involved in checking that what can be done with the data collected not how it has to be done. An implementation of ADT consists of storage structures to store the data items and algorithms for basic operation. All the data structures i.e. array, linked list, stack, queue etc are examples of ADT. Array The simplest type of data structure is a linear (or one dimensional) array. A list of a finite number n of similar data referenced respectively by a set of n consecutive numbers, usually 1, 2,3 . . . . . . . n. if we choose the name A for the array, then the elements of A are denoted by subscript notation A 1, A 2, A 3 . . . . A n

Abstract Data Type-Array

Embed Size (px)

DESCRIPTION

Array,One, Two, Three & Four dimensional Arrays

Citation preview

  • ABSTRACT DATA TYPE ARRAY

    Abstract Data Type It can be defined as a collection of data items together with the operations on the data. The word abstract refers to the fact that the data and the basic operations defined on it are being studied independently of how they are implemented. It involves what can be done with the data, not how has to be done. For ex, in the below figure the user would be involved in checking that what can be done with the data collected not how it has to be done.

    An implementation of ADT consists of storage structures to store the data items and algorithms for basic operation. All the data structures i.e. array, linked list, stack, queue etc are examples of ADT.

    Array The simplest type of data structure is a linear (or one dimensional) array. A list of a finite number n of similar data referenced respectively by a set of n consecutive numbers, usually 1, 2,3 . . . . . . . n. if we choose the name A for the array, then the elements of A are denoted by subscript notation

    A 1, A 2, A 3 . . . . A n

  • or by the parenthesis notation

    A (1), A (2), A (3) . . . . . . A (n)

    or by the bracket notation A [1], A [2], A [3] . . . . . . A [n]

    Example: A linear array A[8] consisting of numbers is pictured in following figure.

    C programming language provides a data structure called the array, which can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

    The array may be categorized into

    One dimensional array Two dimensional array Multidimensional array

  • One-Dimensional Array In Pascal language we can define array as

    VAR X: array [ 1 N] of integer {or any other type} Thats means the structure contains a set of data elements, numbered (N), for example called (X), its defined as type of element, the second type is the index type, is the type of values used to access individual element of the array, the value of index is

    1

  • x number of rows and y number of columns. A 2-dimensional array a, which contains three rows and four columns can be shown as below:

    Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a. A two dimensional m x n Array A is the collection of m X n elements. Programming language stores the two dimensional array in one dimensional memory in either of two ways- Row Major Order: First row of the array occupies the first set of memory locations reserved for the array; Second row occupies the next set, and so forth.

    To determine element address A[i,j]:

    Location ( A[ i,j ] ) =Base Address + ( N x ( I - 1 ) ) + ( j - 1 ) For example: Given an array [15,17] of integers. Calculate address of element T[4,6], where BA=900. Solution:

    I = 4 , J = 6 M= 5 , N= 7

  • Location (T [4,6]) = BA + (7 x (4-1)) + (6-1) = 900+ (7 x 3) +5

    = 900+ 21+5 = 926

    Column Major Order: Order elements of first column stored linearly and then comes elements of next column.

    To determine element address A[i,j]:

    Location ( A[ i,j ] ) =Base Address + ( M x ( j - 1 ) ) + ( i - 1 ) For example: Given an array [16,18] of integers. Calculate address element T[5,7], where BA=300 Solution:

    I = 5 , J = 7 M= 6 , N= 8

    Location (T [4,6]) = BA + (6 x (7-1)) + (5-1) = 300+ (6 x 6) +4

    = 300+ 36+4 = 340

  • Three Dimensional Arrays To calculate address of element X[ i,j,k] using row-major order :

    Location ( X[i,j,k] )=BA + MN (k-1) + N (i-1) + (j-1) using column-major order

    Location ( X[i,j,k] )=BA + MN (k-1) + M (j-1) + (i-1) Four Dimensional Array To calculate address of element X[ i,j,k] using row-major order :

    Location ( Y[i,j,k,l] )=BA + MNR (l-1) +MN (k-1) +N (i-1) + (j-1) using column-major order

    Location ( Y[i,j,k,l] )=BA + MNR (l-1) +MN (k-1) +M (j-1) + (i-1)