DS-2
DS-2
Chittaranjan Pradhan
1D Array
Arrays 2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
2.1
Arrays
Array
Chittaranjan Pradhan
Array Array
1D Array
Matrix Operations
• Arrays are always stored in consecutive memory locations
Dynamic Memory
Allocation
• Array name is actually a pointer to the first location of the
Pointer to Array
memory block allocated to the name of the array
Array of Pointers
• An array can be initialized only during declaration time Pointer to Structure
2.2
Arrays
1D Array
Chittaranjan Pradhan
1D Array
Array
• It is also called as linear array and stores data in a single 1D Array
Matrix Operations
• datatype varname[expression];
Dynamic Memory
int num[10]; Allocation
Pointer to Array
• Size of array in bytes = 10*sizeof(int)
Array of Pointers
• Location for any element of the array can be calculated as Pointer to Structure
address of num[i] = b+w*(i-li), Strings
Array
Traversing
1D Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.4
Arrays
1D Array Operations...
Chittaranjan Pradhan
Insertion
Array
1D Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.5
Arrays
1D Array Operations...
Chittaranjan Pradhan
Deletion Array
1D Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.6
Arrays
1D Array Operations...
Chittaranjan Pradhan
Array
Searching
1D Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.7
Arrays
1D Array Operations...
Chittaranjan Pradhan
Sorting
Array
1D Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.8
Arrays
1D Array Operations...
Chittaranjan Pradhan
Merging
Array
1D Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.9
Arrays
1D Array Operations...
Chittaranjan Pradhan
Q: Find the 3 largest elements in an array of positive integers
Array
1D Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.10
Arrays
2D array
Chittaranjan Pradhan
2D array
Array
• 2D arrays can be thought of as rectangular display of
1D Array
elements with rows and columns 2D array
Ex: int x[3][3]; Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.11
Arrays
2D array
Chittaranjan Pradhan
2D array
Array
Matrix Operations
• Column - Major order Address Calculation Dynamic Memory
Address of A[i][j]=b+w*[(i-lr)+m*(j-lc)] Allocation
Pointer to Array
where b–>base address, w–>memory size of each
Array of Pointers
element, i–>row subscript, j–>column subscript, lr–>start
Pointer to Structure
row index, lc–>start column index, n–> number of columns Strings
of the matrix, m–>number of rows of the matrix Pattern Matching
in the array arr[][] when the array is stored as column Dynamic Memory
Allocation
major wise
Pointer to Array
• A matrix A[m][m] is stored in the memory with each Array of Pointers
1D Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.14
Arrays
Matrix Addition
Chittaranjan Pradhan
Array
1D Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.15
Arrays
Matrix Multiplication
Chittaranjan Pradhan
Array
1D Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.16
Arrays
Matrix Transpose
Chittaranjan Pradhan
Array
1D Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.17
Arrays
Dynamic Memory Allocation
Chittaranjan Pradhan
Dynamic Memory Allocation
Array
• Size of array declared initially can be sometimes
1D Array
insufficient and sometimes more than required. Here, 2D array
memory size can’t be modified while execution Matrix Operations
Pointer to Structure
Strings
Pattern Matching
2.18
Arrays
Dynamic Memory Allocation...
Chittaranjan Pradhan
Dynamic Memory Allocation in 2D Array
Array
• int r=3,c=4;
1D Array
int *ptr=malloc((r*c)*sizeof(int)); 2D array
Matrix Operations
Strings
Pattern Matching
• int r=3,c=4,len;
int *ptr,**arr; <–using double pointer and one malloc
len=sizeof(int*)*r+sizeof(int)*c*r;
arr=(int**)malloc(len);
ptr=(int*)(arr+r); 2.19
Arrays
Pointer to Array
Chittaranjan Pradhan
Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.20
Arrays
Array of Pointers
Chittaranjan Pradhan
Array of Pointers
int *ptr[10]; Array
1D Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.21
Arrays
Pointer to Structure
Chittaranjan Pradhan
Pointer to Structure
The base address of a structure variable is accessed in the Array
same manner like address of any other variable by using & 1D Array
operator 2D array
Dynamic Memory
ptr=&var; Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.22
Arrays
Strings
Chittaranjan Pradhan
Strings
Array
• String is an array of characters with a null character at end
1D Array
char name[size]; 2D array
2.23
Arrays
Strings
Chittaranjan Pradhan
Array
1D Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.24
Arrays
Pattern Matching
Chittaranjan Pradhan
Pattern Matching
It is the process of searching a given pattern P in a text S. Array
2D array
Matrix Operations
Dynamic Memory
Allocation
Pointer to Array
Array of Pointers
Pointer to Structure
Strings
Pattern Matching
2.25