Data Structures
Data Structures
IOT
DATA
STRUCTURES
MODULE-1: ARRAYS
1
ARRAY
S▶ ARRAYS
▶ DECLARATION OF ARRAYS
▶ INITIALIZATION OF ARRAYS
▶ BOUND CHECKING
ARRAY
▶ SArray is a data structure that can store a fixed-size sequential collection of elements of
the same type.
▶ An array is defined as finite ordered collection of homogenous data (of same type),
stored in contiguous memory locations.
▶ The lowest address corresponds to the first element and the highest address to the last
element.
4
INITIALIZATION of ARRAYS
▶ We can initialize an array in ‘C’ either one by one or using a single statement
as:
char cName[30];
▶ Example for
compile time:
#include<stdio.h>
void main( )
{
int i;
int arr[ ] = {2, 3, 4};
//Compile time array
initialization
printf("%d\t", arr[i]);
for(i = 0 ; i < 3 ; i++) Output:
}
{
} 234
6
INITIALIZATION of ARRAYS (Contd.)
▶ Example for run time:
#include<stdio.h>
void main( )
{
int arr[3]; int i, j;
printf("Enter array element");
for(i = 0; i < 3; i++)
{
scanf("%d", &arr[i]); //Run time array initialization
}
for(j = 0; j < 3; j++)
Output:
{
printf("%d\n", arr[j]); 234
}
7
}
Example – 1: Display ARRAY ELEMENTS
#include <stdio.h>
int main ( )
{
int n[ 10 ]; /* n is an array of 10 integers
*/
int i, j; /* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ )
{
Output:
n[i] = i + 100; /* set element at location i to i + Element[0] = 100
100 */ Element[1] = 101
} /* output each array element's value */ Element[2] = 102
Element[3] = 103
for (j = 0; j < 10; j++ ) Element[4] = 104
{ Element[5] = 105
Element[6] = 106
printf("Element[%d] = %d\n", j, n[j] ); Element[7]8 = 107
Element[8] = 108
} return 0;
Element[9] = 109
Different ways of initialization of Arrays
(a) Initializing all specified memory locations
9
(a) Initializing all specified memory
location
▶ Arrays can be initialized at the time of declaration when their initial
values are known in advance.
for the variable ‘a’ and all these locations are initialized as shown in
figure.
(b) Partial array initialization
▶ If the number of values to be initialized is less than the size of the
array, then the elements will be initialized to zero automatically.
▶ So, the array size will be set to 8 automatically. The array ‘b’ is initialized
as shown in figure. 12
(d) Array initialization with a string
▶ Consider the declaration with string
initialization.
▶ So, the array size is 9 bytes (i.e., string length 1 byte for null character).
13
DIMENSIONS
14
ONE-DIMENSIONAL & TWO-DIMENSIONAL
ARRAYS
(a) One-dimensional arrays:
▶ A one-dimensional array (or single dimension array) is a type of linear array. An array
with a single subscript is known as one dimensional array.
▶ Accessing its elements involves a single subscript which can either represent a row or
column index.
▶ Syntax:
▶ Example: The array can contain 10 elements of any value available to the various data
type is :
int a[10];
float a[10];
15
double a[10.0];
ONE-DIMENSIONAL & TWO-DIMENSIONAL
ARRAYS
(b) Two-dimensional arrays:
▶ An array consisting of two subscripts is known as two-dimensional array.
▶ In two dimensional arrays, the array is divided into rows and columns.
▶ Syntax:
▶ Example: int a[3][3]; where first index value shows the number of the rows and
second index value shows the number of the columns in the array.
16
MULTI-DIMENSIONAL
ARRAYS
(c) Multi-dimensional arrays:
▶ In multidimensional arrays the array is divided into rows and columns, mainly applicable for
3-D array, 4-D arrays., etc.
▶ Syntax:
17
SIMPLE OPERATIONS –SEARCHING
▶ Searching Algorithms are designed to check for an element or retrieve an
element from any data structure where it is stored.
▶ Searching Techniques:
Linear Search
Binary Search
18
Linear Search
▶ Linear search is a very simple and basic search algorithm.
▶ It checks each element of the list sequentially until a match is found or the whole list has
been searched.
▶ The time required to search an element using a linear search algorithm depends on the
size of the list.
19
Binary Search
▶ Binary search is the most popular Search algorithm.
▶ It is efficient and also one of the most commonly used techniques that is used to solve
problems.
▶ The time complexity of a binary search is O(log n).
20
SIMPLE OPERATIONS –Matrix Operations
▶ Matrix operations:
Matrix Addition / Matrix Subtraction
Matrix Multiplication
21
Matrix Addition
▶ Matrix addition in C language to add two matrices, i.e., compute their sum
and print it.
▶ A user will input the order of matrix (number of rows and columns) and two
matrices.
▶ For example, if the order is 2, 2, i.e., two rows and two columns and the
matrices are:
First matrix: 1 2
3 4
Second matrix: 4 5
-1 5
Output
1 + 4 will be: 5 7
2 9 22
2+5
Matrix Multiplication
▶ Matrix multiplication in C language to calculate the product of two matrices
(two-dimensional arrays).
23
Memory
Allocation
▶ Memory allocation is a process by which computer programs and
services are assigned with physical or virtual memory space.
24
Static Memory Allocation Dynamic Memory Allocation
It uses stack for managing the It uses heap for managing the dynamic
static allocation of memory. allocation of memory.
int a = 10;
int array[10];
26
Dynamic Memory
▶Allocation
Memoryallocation done at the time of
execution(run time) is known as dynamic memory allocation.
27
Dynamic Memory
▶Allocation
Allocating memory in run time:
malloc( ):
Syntax: (typecast)malloc(size)
Example: ptr = (int *) malloc(n * size(int))
28
Dynamic Memory
Allocation
Realloc( ): reallocation(increase the existing memory)
29
Bound Checking
▶ Bounds checking is any method of detecting whether
a variable is within some bounds before it is used.
30
Bound Checking
▶ Bounds checking is any method of detecting whether
a variable is within some bounds before it is used.
31
Example Programs for practice
Program example 1: Sum of array’s element.
Program example 3: Searching the biggest value. By modifying the previous example we
can
search the biggest value.
Program example 4: Searching the location for the given value in an 32
array