Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Department of Computer Science & Engineering

IOT

DATA
STRUCTURES

MODULE-1: ARRAYS

1
ARRAY
S▶ ARRAYS

▶ DECLARATION OF ARRAYS

▶ INITIALIZATION OF ARRAYS

▶ ONE-DIMENSIONAL & TWO-DIMENSIONAL ARRAYS

▶ SIMPLE MATRIX OPERATIONS – ADDITION & MULTIPLICATION

▶ STATIC & DYNAMIC


2

▶ 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.

▶ Examples where arrays are applicable:


 Used to store list of Employee or Student names.
 Used to store marks of students,
 Or to store list of numbers or characters etc.
3
DECLARATION of ARRAYS
▶ General form of array declaration
is:
data-type array_name[array_size];
/* Example of array declaration */
int studMark[1000];
 This will reserve 1000 contiguous memory locations for storing the students’
marks.
 Graphically, this can be depicted as in the following figure.

4
INITIALIZATION of ARRAYS
▶ We can initialize an array in ‘C’ either one by one or using a single statement
as:

char cName[30];

 In this statement, the array character can store up to 30


characters with the first character occupying location cName[0]
and the last character occupying cName[29].
 Note that the index runs from 0 to 29. In C, an index always
starts from 0 and ends with array's (size-1).
 So, take note the difference between the array size and
subscript/index terms.
5
INITIALIZATION of ARRAYS (Contd.)
▶ An array can be initialized at either compile time or at run time.

▶ 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

(b) Partial array initialization

(c) Initialization without size

(d) Array initialization with a string

9
(a) Initializing all specified memory
location
▶ Arrays can be initialized at the time of declaration when their initial
values are known in advance.

▶ Array elements can be initialized with data items of type int,


char
etc.

▶ Ex:- int a[5] = {10, 15, 1, 3, 20};

a[0] a[1] a[2] a[3] a[4]


10 15 1 3 20
1000 1002 1004 1006 1008

▶ During compilation, 5 contiguous memory locations are reserved by the compiler


10

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.

▶ Ex:- int a[5] = {10, 15};

a[0] a[1] a[2] a[3] a[4]


10 15 0 0 0
1000 1002 1004 1006 1008

▶ Eventhough compiler allocates 5 memory locations, using this declaration


statement; the compiler initializes first two locations with 10 and 15, the
next set of memory locations are automatically initialized to 0's by
compiler as shown in figure.
11
(c) Initialization without size
▶ Consider the declaration along with the initialization:

▶ Ex:- char b[ ] = {'C','O','M','P','U','T','E','R'};

b[0] b[1] b[2] b[3] b[4] B[5] b[6] b[7]


C O M P U T E R
1000 1001 1002 1003 1004 1005 1006 1007

▶ In this declaration, eventhough we have not specified exact number of


elements to be used in array ‘b’, the array size will be set of the total
number of initial values specified.

▶ 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.

▶ Ex:- char b[ ] = "COMPUTER";


b[1] b[2] b[3] b[4] B[5] b[6] b[7] b[8]
b[0]
C O M P U T E R \0
1000 1001 1002 1003 1004 1005 1006 1007 1008

▶ Eventhough the string "COMPUTER" contains 8 characters, because it is a string, it


always ends with null character.

▶ 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:

data type array_name[array_size];

▶ 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.

▶ These are often known as array of the array.

▶ In two dimensional arrays, the array is divided into rows and columns.

▶ Syntax:

Data type array_name[array_size][column_size];

▶ 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:

Data type array_name[size1][size2][size3]…….…[sizeN];

▶ Example: int a[2][3][4];


float a[5][4][5][3];

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.

▶ A linear search, also known as a sequential search, is a method of finding an element


within a list.

▶ 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.

▶ The time complexity of a linear search is O(n).

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

Add first & Second


matrix:

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).

▶ A user inputs the orders and elements of the matrices.

▶ If the multiplication isn't possible, an error message is displayed.

23
Memory
Allocation
▶ Memory allocation is a process by which computer programs and
services are assigned with physical or virtual memory space.

▶ There are two types of memory allocations:

 Compile-time or Static Memory Allocation


 Run-time or Dynamic Memory Allocation

24
Static Memory Allocation Dynamic Memory Allocation

In the static memory allocation, In the Dynamic memory allocation,


variables get allocated permanently, till variables get allocated only if your
the program executes or function call program unit gets active.
finishes.
Static Memory Allocation is done before Dynamic Memory Allocation is
program execution. done during program execution.

It uses stack for managing the It uses heap for managing the dynamic
static allocation of memory. allocation of memory.

It is less efficient. It is more efficient.

In this memory is allocated at compile In this memory is allocated at run time.


time.
Example: This static memory allocation Example: This dynamic
is generally used for array. memory 25
allocationisgenerallyusedforlinked
Static Memory
Allocation
▶ Static Memory is declared variables by the
allocated for compiler.

 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.

▶ Allocating memory in run time:


 malloc( )
 calloc()
 realloc( )
 free( )

27
Dynamic Memory
▶Allocation
Allocating memory in run time:
 malloc( ):
Syntax: (typecast)malloc(size)
Example: ptr = (int *) malloc(n * size(int))

 calloc( ): Continuous memory allocation in terms of


blocks. Syntax: ptr = (typecast)calloc(number, size)
Example: ptr = (int*)calloc(5, size(int));

28
Dynamic Memory
Allocation
 Realloc( ): reallocation(increase the existing memory)

Syntax: ptr = realloc(ptr, new_size);


Example: ptr = realloc(ptr, 10);

 free( ): for deallocating (delete the


memory) Syntax: free(ptr);

29
Bound Checking
▶ Bounds checking is any method of detecting whether
a variable is within some bounds before it is used.

▶ It is usually used to ensure that a number fits into a given type


(range checking), or that a variable being used as an array index
is within the bounds of the array (index checking).

▶ A failed bounds check usually results in the generation of some


sort of exception signal.

30
Bound Checking
▶ Bounds checking is any method of detecting whether
a variable is within some bounds before it is used.

▶ It is usually used to ensure that a number fits into a given type


(range checking), or that a variable being used as an array index
is within the bounds of the array (index checking).

▶ A failed bounds check usually results in the generation of some


sort of exception signal.

31
Example Programs for practice
 Program example 1: Sum of array’s element.

 Program example 2: Searching the smallest value.


 Finding the smallest element in the array named fSmallest.
 First, it assumes that the smallest value is in fSmallest[0] and assigns it to the variable
nSmall.
 Then it compares nSmall with the rest of the values in fSmallest, one at a time.
 When an element is smaller than the current value contained in nSmall, it is assigned
to nSmall. The process finally places the smallest array element in nSmall.

 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

You might also like