0% found this document useful (0 votes)
25 views25 pages

DS-2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views25 pages

DS-2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Arrays

Chittaranjan Pradhan

Data Structures 2 Array

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

• It is a collection of homogeneous elements 2D 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

• If we initialize the first element of an array with a value Strings


Pattern Matching

during the declaration statement, all other elements are


automatically initialized to zero

• Arrays are widely used to implement mathematical


vectors, matrices etc
• Arrays are used to implement other data structures such
as strings, stacks, queues etc.

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

row or column 2D 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

where b–>base address, w–>memory size of each Pattern Matching

element, i–>subscript of element, li->first index


• Ex: In an array, A[-10...2], Base address=1001, size of an
element = 2B, find the location of A[-1]

• Arrays can be passed to function by call by reference


using array name written inside the argument list
sum=funarray(a,9);
int funarray(int p[], int n);
2.3
Arrays
1D Array Operations
Chittaranjan Pradhan

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

• Row - Major order Address Calculation 1D Array

Address of A[i][j]=b+w*[n*(i-lr)+(j-lc)] 2D 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

• Ex: A matrix A[-4...6,3...8] is stored in the memory with


each element requiring 4B of storage. If the base address
is 1430, find the address of A[3][6] when the matrix is
stored in row-major form
• Ex: A square matrix M[][] of size 10 is stored in memory
with each element requiring 4B of storage. If the base
address at M[0][0] is 1840, determine the address at
M[4][8] when the matrix is stored in row-major wise
2.12
Arrays
2D array...
Chittaranjan Pradhan
2D array...
Array
• Each element of an array arr[15][20] requires ’W’ bytes of
1D Array
storage. If the address of arr[6][8] is 4440 and the base 2D array
address at arr[1][1] is 4000, find the width ’W’ of each cell Matrix Operations

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

element requiring 4 bytes of storage. If the base address Pointer to Structure

at A[1][1] is 1500 and the address of A[4][5] is 1608, Strings


Pattern Matching
determine the order of the matrix when it is stored in
Column Major Wise
• A matrix P[15][10] is stored with each element requiring 8
bytes of storage. If the base address at P[0][0] is 1400,
determine the address at P[10][7] when the matrix is
stored in Row Major Wise
• A matrix A[m][n] is stored with each element requiring 4
bytes of storage. If the base address at A[1][1] is 1500 and
the address at A[4][5] is 1608, determine the no. of rows of
the matrix when the matrix is stored in Column Major Wise 2.13
Arrays
2D array...
Chittaranjan Pradhan
Q: Find sum of each row and column of a matrix
Array

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

• The process of allocating memory during program Dynamic Memory


Allocation
execution is called dynamic memory allocation. Here, Pointer to Array
memory size can be modified while execution Array of Pointers

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

• int r=3, c=4; Dynamic Memory


Allocation
int *arr[r]; <–using array of pointers Pointer to Array
for(i=0;i<r;i++) Array of Pointers

arr[i]=(int*)malloc(c*sizeof(int)); Pointer to Structure

Strings
Pattern Matching

• int r=3,c=4; <–using pointer to a pointer


int** arr=(int**)malloc(r*sizeof(int*));
for(i=0;i<r;i++)
arr[i]=(int*)malloc(c*sizeof(int));

• 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

Pointer to Array 1D Array

2D array

Matrix Operations

Dynamic Memory
Allocation

Pointer to Array

Array of Pointers

Pointer to Structure

Strings
Pattern Matching

The increment in the pointer leads to increment in the address


of elements of matrix (row wise)

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

type *ptr; Matrix Operations

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

• Commonly used string manipulation functions: Matrix Operations

• strlen(s) Dynamic Memory


Allocation
• strcat(s1,s2) Pointer to Array
• strcpy(s1,s2) Array of Pointers
• strcmp(s1,s2) Pointer to Structure
• strrev(s) Strings
Pattern Matching

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

Commonly used algorithm is Brute-Force matching algorithm 1D Array

2D array

Matrix Operations

Dynamic Memory
Allocation

Pointer to Array

Array of Pointers

Pointer to Structure

Strings
Pattern Matching

2.25

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy