1. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
Data Structure
Mr. Prince Kumar
CSIT Department
2. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Array:-
• Array is a collection of similar types of elements
which stored in a contiguous memory location.
• Here multiple items are stored together but of
same type
• In array random access is possible.
3. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Example
• Without array:
• int a=10; int b=20; int a=30; int b=40; int a=50;
int b=60; int a=70; int b=80;
• With array:
• Syntax of array in C language:
datatype name_of_array[size]={element1, element2.....element_n}
Above example using array
int arr[8]={10,20,30,40,50,60,70,80}
4. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Indexing is used in array to represent position of
element in array.
• Types of indexing in an array:
• 0 (zero-based indexing): The first element of the
array is indexed by a subscript of 0
• 1 (one-based indexing): The first element of the
array is indexed by the subscript of 1
• n (n-based indexing): The base index of an array can
be freely chosen. Usually, programming languages
allowing n-based indexing also allow negative index
values, and other scalar data types like
enumerations, or characters may be used as an
array index.
6. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• To access array elements syntax is:
• arr_name[element_index_value];
Example:
int arr[5]={1,2,3,4,5};
//index= 0,1,2,3,4
To Access
arr[4];
7. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Types of arrays
• Arrays are broadly classified into three types.
They are as follows −
• One – dimensional arrays
• Two – dimensional arrays
• Multi – dimensional arrays
8. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• One – dimensional array
• The Syntax is as follows −
• datatype array_name [size];
• For example, int a[5]={10,20,30,40,50}
9. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Example
• Following is the C program in which array is used
#include<stdio.h>
int main ( )
{
int a[5] = {10,20,30,40,50};
printf ("elements of the array are");
for ( int i=0; i<5; i++)
{
printf ("%d", a[i]);
}
return 0;
}
10. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Numerical Problem on 1 Dimensional Array:
Problem:- Using random access find i’th element
address of array.
Q)A[lb............ub],BA,Size of element=s,Loc(a[i])=?
Formula used:-
A[i]=BaseAddress+(i-lowerbound)*s
Find i’th
element
address
It is first
address
of array
i is index
value of
which
we want
to find
address
Starting
index
Size of
each
element
in array
11. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Problem 1
int a[10];
int a[0.......9]={10,20,30,40,50,60,70,80,90,100}
• Loc(a[5])=?
• Solution
• Loc(a[5])=100+(5-0)*2=110
Upper
bound
Lower
bound
12. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Problem 2
• Loc(a[9])=?
• Array Representation(For Verification of answer)
index 0 1 2 3 4 5 6 7 8 9
Arr_name 10 20 30 40 50 60 70 80 90 100
Address 100 102 104 106 108 110 112 114 116 118
13. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
Problem 3
A[24............900],B.A=500,Size of element=8
Loc(a[625])=?
Problem 4
A[-65.............+973],BA=1000,Size of element=6
Loc(A[725])==?
14. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Note:-
• Array index by default start from 0(zero) because
no need to perform extra subtraction operation
otherwise we need to perform one addition
subtraction.
15. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• 2 Dimensional Array:
• The two-dimensional array can be defined as an
array of arrays.
• Data in multidimensional arrays are stored in
tabular form (in row major order).
• Two dimensional array will be stored in the
memory in the form of multiple 1 dimensional
array.
16. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• They will be stored in the two different ways:
• A) Row major order
• B)Column major order
• C language by default follows Row major order
17. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• General form of declaring N-dimensional arrays:
data_type array_name[size1][size2]....[sizeN];
data_type: Type of data to be stored in the array.
Here data_type is valid C/C++ data type
array_name: Name of the array
size1, size2,... ,sizeN: Sizes of the dimensions
18. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Examples:
• Two dimensional array: int two_d[10][20];
• Three dimensional array: int
three_d[10][20][30];
19. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• We can declare a two dimensional integer array
say ‘x’ of size 10,20 as:
int x[10][20];
Elements in two-dimensional arrays are
commonly referred by x[i][j] where i is the row
number and ‘j’ is the column number.A two –
dimensional array can be seen as a table with ‘x’
rows and ‘y’ columns where the row number
ranges from 0 to (x-1) and column number
ranges from 0 to (y-1).
20. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• A two – dimensional array ‘x’ with 3 rows and 3
columns is shown below:
21. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Initializing Two – Dimensional Arrays: There are
two ways in which a Two-Dimensional array can be
initialized.
First Method:
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
The above array have 3 rows and 4 columns. The
elements in the braces from left to right are stored
in the table also from left to right. The elements will
be filled in the array in the order, first 4 elements
from the left in first row, next 4 elements in second
row and so on.
22. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Better Method:
int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
This type of initialization make use of nested braces.
Each set of inner braces represents one row. In the
above example there are total three rows so there
are three sets of inner braces.
To access elements of 2d array:-
For example to access 2nd element of 3nd row
X[2][1];
23. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• To output all the elements of a Two-Dimensional
array we can use nested for loops. We will
require two for loops. One to traverse the rows
and another to traverse columns.
24. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
#include<stdio>
int main()
{
int x[3][2] = {{0,1}, {2,3}, {4,5}};
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
printf(“%d”,x[i][j]);
}
printf(“n”);
}
return 0;
}
25. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Output:-
0 1
2 3
4 5
26. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• We can represent 2d array in two ways:
• 1) Row major order
• Int a[1.....4][1.......5]
Number of rows= ub1-lb1+1
Number of columns= ub2-lb2+1
Lower
bound1
Upper
bound1
Lower
bound 2
Upper
bound 2
27. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Problems
• Q) a[lb1.........ub1][lb2...........ub2],BA,Size of
element =s,loc(a[i][j])=?
• Formula:
• loc(a[i][j])=? BA+((a[i]-lb1)*(ub2-lb2+1)+j-lb2)*s
Find i’th
row, j’th
column
element
address
It is first
address
of array
Number
of
column Size of
each
element
in array
28. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Problem 1)
• Int a[1.....4][1.......5]
• BA=100, Size of element =2
• Loc(a[4][3])=?
• Problem 2
• Problem 2)
• Loc(a[2][5])=?
29. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Problem 3)
• A[75.....198][-21.........+155],BA=500,Size of
element=8
• Loc(a[100][103])=?
30. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• 2) column major order
• In this element are stored column wise
31. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Problems
• Q) a[lb1.........ub1][lb2...........ub2],BA,Size of
element =s,loc(a[i][j])=?
• Formula:
• loc(a[i][j])=? BA+((a[j]-lb2)*(ub1-lb1+1)+j-lb1)*s
Find i’th
row, j’th
column
element
address
It is first
address
of array
Number
of rows
Size of
each
element
in array
32. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Problem 1
• Int a[1.....4][1.......5]
• BA=100, Size of element =2
• Loc(a[3][5])=?
• Problem 2
• Loc(a[3][3])=?
33. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Application of Arrays:
• Arrays are the simplest data structures that
stores items of the same data type. A basic
application of Arrays can be storing data in
tabular format. For example, if we wish to store
the contacts on our phone, then the software
will simply place all our contacts in an array.
• 2D arrays-are used to implement mathematical
vectors and matrices.
34. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Arrays are used to implement other data
structures, such as lists, heaps, hash tables,
deques, queues, stacks, strings
• All sorting algorithms use arrays at its core
35. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Arrays are used to implement vectors and lists
which are an important part of C++ STL.
• The Standard Template Library (STL) is a set of
C++ template classes to provide common
programming data structures and functions such
as lists, stacks, arrays, etc
• Arrays are also used to implement stack and
queues.
36. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Matrices which are an important part of the
mathematical library in any programming
languages is implemented using arrays.
• CPU scheduling algorithms use implemented
using arrays.
• All sorting algorithms use arrays at its core.
37. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
Sparse Matrix and its
representations
38. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• A matrix is a two-dimensional data object made
of m rows and n columns, therefore having total
m x n values. If most of the elements of the
matrix have 0 value, then it is called a sparse
matrix.
39. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
Why to use Sparse Matrix instead of simple
matrix ?
• Storage: There are lesser non-zero elements
than zeros and thus lesser memory can be used
to store only those elements.
• Computing time: Computing time can be saved
by logically designing a data structure traversing
only non-zero elements
40. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Example:
0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0
41. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• Representing a sparse matrix by a 2D array leads
to wastage of lots of memory as zeroes in the
matrix are of no use in most of the cases. So,
instead of storing zeroes with non-zero
elements, we only store non-zero elements. This
means storing non-zero elements with triples-
(Row, Column, value).
42. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
Sparse Matrix Representations can be done
in many ways following are two common
representations:
1. Array representation
2. Linked list representation
43. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
• 2D array is used to represent a sparse matrix in which there are three
rows named as:
• Row: Index of row, where non-zero element is located
• Column: Index of column, where non-zero element is located
• Value: Value of the non zero element located at index – (row , column)
Triplet as - (Row, Column, value)
Method 1: Triplet Representation
(Array Representation)
44. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
Consider the following as an example of a sparse matrix B:
45. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
Method 2: Linked List Representation
• In linked list, each node has four fields. These four fields are defined
as:
• Row: Index of row, where non-zero element is located
• Column: Index of column, where non-zero element is located
• Value: Value of the non zero element located at index – (row ,
column)
• Next node: Address of the next node
47. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
N Dimensional Array Address Calculation
48. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
Array is A(m1,m2,....mn)
We have to find address of A[k1][k2]...[kn]
Where 1<=k1<m1,
1<=k2<m2,
1<=kn<mn
Li=UB-LB+1 (Li is length of ith dimension)
and
Ei=Ki-LB (Where Ei is effective address(It is distance from lower bound to
ith subscript))
Row major order
Address of A[k1 k2 .....kn]=BA+W*[(((E1*L2+E2)L3+E3)L4......En-1)Ln+En]
49. Department of Computer Science &
Information Technology
Prince Kumar
Data Structure
Address of A[k1 k2 .....kn]=BA+W*[(((E1*L2+E2)L3+E3)L4......En-1)Ln+En]
Example:-
M(2:8, -4:1, 6:10)
Address of M[5,-1,8]=?
B.A=200
L1=8-2+1=7
L2=1-(-4)+1=6
L3=10-6+1=5
E1=5-2=3
E2=-1-(-4)=3
E3=8-6=2
E1*L2=3*6=18
E1*L2+E2=18+3=21
(E1*L2+E2)L3=21*5=105
(E1*L2+E2)L3+E3=105+2=107
M[5,-1,8]=200+4*107=628