Arrays: by Dr.A.Ashok Kumar Deparment of Computer Science Alagappa Government Arts College, Karaikudi
Arrays: by Dr.A.Ashok Kumar Deparment of Computer Science Alagappa Government Arts College, Karaikudi
By
Dr.A.Ashok Kumar
Deparment of Computer Science
Alagappa Government Arts College, Karaikudi
Introduction
An array refers to a collection of homogeneous
elements, stored in contiguous locations in
memory
The basic operations on arrays:
• Extraction and
• Storing
Array is an example of static allocation of
memory
Continued...
A variable of primitive type refer a single
entity
Arrays are data structures used to refer a
collection of similar data types
representation of an 1010
B
1010
array 1010
C
1010 D
T
Range of an Array
Individual elements of an array are referred with
the index
Index gives the relative position of the element
(offset) from the starting position of the array
The first index is referred as the lower bound
The last index is referred as the upper bound
Range refers to the number of elements in the
array
The lower bound depends on programming
languages
Continued..
Few examples of array declaarations in C:
• Int data[10]
Lower bound 0, upperbound 9, range 10
• Char code[20]
Lower bound 0, upper bound 19, range 20
In general, if the lower bound is 0 and range is n
then the upper bound will be n-1
The physical size of the array is:
the number of elements * number of bytes
required to store the element
Primitive operations
The primitive operations on an array are
• Extraction(retrieval)
• Storing
Extraction refers to getting the value of an
element stored in an array
X = Data[i] , where i is the valid index in the
array
Storing value is opposite to retrieval
Data[i] = X
Addressing Function
A function that gives the starting byte address
of an element, if the base address and index of
the element are given
Addressing functions give a mapping between
logical numbering of the elements and the
physical byte address
Addressing function depends on the type of the
arrays
One-dimensional array
It refers to a collection of elements in one
dimension
str
A B C D E F G H I J
10 elements
Char str[10];
Continued...
If base address of str be b
• Then the first element stored in this address
• Second element stored in b+2
• Third element stored in b+2*2
We assume elements are stored from 0th index
In general
• The address of the ith element is b+i*e
b is the base address
i is the index of the element
e is the size of the data type
This is the addressing function of one-dimensional
array
Two-dimensional array
Represented by rows and columns
Individual elements denoted by row index and
column index values
Two-dimensional matrix id declared as
A[lb1...ub1] [lb2...ub2]
lb1 and lb2, lower bound for rows and columns
ub1 and ub2, upper bound for rows and columns
The range of 2-dimensional array is (ub1-lb1+1)
(ub2-lb2+1)
Storage representation of 2D arrays
2D arrays are logical representation
The storage is single dimension
So need a mapping between 2D representation
and single dimension
This mapping is a way of storing the elements
of 2D array into an one-dimensional array
Two ways of storage representations:
1. Row major order representation
2. Column major order representation
Row major order representation
The elements are stored in row wise
The elements of 0th row are stored first and then
first row and so on
Address Values
1 2 3 4 5 b 1
b+1 2
6 7 8 9 10 b+2 3
b+3 4
11 12 13 14 15
b+4 5
b+5 6
b+6 7
b+7 8
b+8 9
b+9 10
b+10 11
b+11 12
b+12 13
b+13 14
b+14 15
Continued...
The addressing function is:
Address of a[i][j] = b+[(i*COL)+j]*e
• b is base address
• i number of elements in each row
• COL number of columns
• j is the number of elements in the ith row and column
preceding j
• e is size of each element
Column major order representation
The elements are stored in column wise
The elements in 0th column are stored first, then 1st column and
so on
Address Values
1 2 3 4 5 b 1
b+1 6
6 7 8 9 10 b+2 11
b+3 2
11 12 13 14 15 b+4 7
b+5 12
b+6 3
b+7 8
b+8 13
b+9 4
b+10 9
b+11 14
b+12 5
b+13 10
b+14 15
Continued...
The addressing function is
Address of a[i][j] = b+[(j*ROW)+i)*e
• b is base address
• j Index of jth column
• ROW number of rows in the table
• i index of ith row
• e is the size of each column
Special types of matrices
The special types of matrices classified based
on the characteristic position of non zero
elements
• Lower triangular matrices
• Upper triangular matrices
• Tridiagonal Matrices
• Sparse matrices
Lower triangular Matrices
A square matrix is called lower triangular if
all the entries above the main diagonal are zero
1 0 0 0
2 3 0 0
4 5 6 0
7 8 9 10
In a n*n lower triangular matrix, the number of
non-zero elements is
1+2+3+...+n = n(n+1)/2
Continued...
In a lower triangular matrix,
0 If i>j
A[i][ j ]
non zero otherwise
B[i(i 1) / 2 j If i≤j
A[i][ j ]
0 otherwise
Upper triangular matrix
A square matrix is called upper triangular if
all the entries below the main diagonal are zero
1 2 3 4
0 5 6 7
0 0 8 9
0 0 0 10
In a n*n lower triangular matrix, the number of
non-zero elements is
n+(n-1)+(n-2)+....+2+1=n(n+1)/2
Continued ...
In an upper triangular matrix
0 If i<j
A[i][ j ]
non zero otherwise
B[i(i 1) / 2 j If i≥j
A[i][ j ]
0 otherwise
Tridiagonal Matrix
A tridiagonal matrix has elements only on the
main diagonal, the first superdiagonal, and the
first subdiagonal
x x 0 0
x x x 0
0 x x x
0 0 x x
A[i][j] = B[2*i+j]
Sparse matrix
Sparse matrices in which the number of non-zero
elements is less compared to zero elements
The non-zero elements will be in random positions
Each element of a sparse matrix is represented by a
triple as (row,col,value)