0% found this document useful (0 votes)
7 views

Array

array

Uploaded by

abhay sharma
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)
7 views

Array

array

Uploaded by

abhay sharma
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/ 20

Understanding of Arrays

Anish Kumar Yadav

July 11, 2024

Anish Kumar Yadav Understanding of Arrays July 11, 2024 1 / 20


Introduction to Arrays in C

Definition: Arrays are collections of elements of the same type stored


in contiguous memory locations.
Importance: Fundamental for efficient data storage and manipulation
in C programming.

Anish Kumar Yadav Understanding of Arrays July 11, 2024 2 / 20


Characteristics of Arrays

Fixed Size: Size is fixed at compile-time.


Homogeneous Elements: All elements are of the same data type.
Indexed Access: Elements can be accessed using their index.

Anish Kumar Yadav Understanding of Arrays July 11, 2024 3 / 20


Array Declaration and Initialization in C

i n t a r r [ 5 ] = {1 , 2 , 3 , 4 , 5};

Anish Kumar Yadav Understanding of Arrays July 11, 2024 4 / 20


Accessing Array Elements in C

i n t v a l u e = a r r [ 2 ] ; // A c c e s s i n g t h e t h i r d e l e m e n t ( i n

Anish Kumar Yadav Understanding of Arrays July 11, 2024 5 / 20


Array Traversal in C
// D i s p l a y i n g a l l e l e m e n t s i n t h e a r r a y
void d i s p l a y A r r a y ( i n t a r r [ ] , i n t s i z e ) {
f o r ( i n t i = 0 ; i < s i z e ; i ++) {
p r i n t f ( ”%d ” , a r r [ i ] ) ;
}
p r i n t f ( ”\n” ) ;
}

// Example o f a r r a y t r a v e r s a l
i n t main ( ) {
i n t a r r [ ] = {3 , 1 , 4 , 1 , 5};
int size = 5;

p r i n t f ( ” Array elements : ” ) ;
displayArray ( arr , s i z e ) ;

return 0;
Anish Kumar Yadav Understanding of Arrays July 11, 2024 6 / 20
Insertion and Deletion in Arrays in C
Insertion: Add a new element at a specific position.
void i n s e r t E l e m e n t ( i n t a r r [ ] , i n t ∗ s i z e , i n t p o
if ( position < 0 || position > ∗ size ) {
p r i n t f ( ” I n v a l i d p o s i t i o n ! \ n” ) ;
return ;
}
// S h i f t e l e m e n t s t o make s p a c e f o r t h e new
f o r ( i n t i = ∗ s i z e − 1 ; i >= p o s i t i o n ; i −−)
arr [ i + 1] = arr [ i ] ;
}
arr [ position ] = value ;
( ∗ s i z e )++;
}
Deletion: Remove an element from a specific position.
void d e l e t e E l e m e n t ( i n t a r r [ ] , i n t ∗ s i z e , i n t p
i f ( p o s i t i o n < 0 | | p o s i t i o n >= ∗ s i z e ) {
Anish Kumar Yadav Understanding of Arrays July 11, 2024 7 / 20
Examples of Insertion and Deletion

Inserting an element at position 2 in array arr:


Initial array: [3, 1, 4, 1, 5]
After insertion of 9 at index 2: [3, 1, 9, 4, 1, 5]
Deleting element at position 3 in array arr:
Initial array: [3, 1, 4, 1, 5]
After deletion: [3, 1, 4, 5]

Anish Kumar Yadav Understanding of Arrays July 11, 2024 8 / 20


Searching Algorithms for Arrays in C

Linear Search
Binary Search

Anish Kumar Yadav Understanding of Arrays July 11, 2024 9 / 20


Examples of Searching Algorithms

Performing linear search for element 4 in array arr:


Array: [3, 1, 4, 1, 5]
Result: Element 4 found at index 2.
Performing binary search for element 5 in sorted array arr:
Sorted array: [1, 1, 3, 4, 5]
Result: Element 5 found at index 4.

Anish Kumar Yadav Understanding of Arrays July 11, 2024 10 / 20


Sorting Algorithms for Arrays in C

Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort

Anish Kumar Yadav Understanding of Arrays July 11, 2024 11 / 20


Examples of Sorting Algorithms

Sorting array arr using bubble sort:


Unsorted array: [3, 1, 4, 1, 5]
After sorting: [1, 1, 3, 4, 5]
Sorting array arr using selection sort:
Unsorted array: [3, 1, 4, 1, 5]
After sorting: [1, 1, 3, 4, 5]

Anish Kumar Yadav Understanding of Arrays July 11, 2024 12 / 20


Multi-Dimensional Arrays in C

Definition: Arrays with more than one dimension (e.g., 2D, 3D


arrays).
Example: 2D array as a matrix.
int matrix [3][3] = {
{1 , 2 , 3} ,
{4 , 5 , 6} ,
{7 , 8 , 9}
};

Anish Kumar Yadav Understanding of Arrays July 11, 2024 13 / 20


Calculating Array Element Location

Row-Major Order:
Formula:
loc = base address + (row × num cols + col) × sizeof(element type)
Column-Major Order:
Formula:
loc = base address + (col × num rows + row ) × sizeof(element type)

Anish Kumar Yadav Understanding of Arrays July 11, 2024 14 / 20


Example Calculation

Given a 2D array arr[3][4]:


Row-Major: loc = 1000 + (1 × 4 + 2) × 4 = 1024
Column-Major: loc = 1000 + (2 × 3 + 1) × 4 = 1028
Therefore, the location of arr[1][2] in row-major order is 1024.

Anish Kumar Yadav Understanding of Arrays July 11, 2024 15 / 20


Advantages of Row-Major and Column-Major Order

Row-Major: Benefits from spatial locality in row-wise data access


patterns.
Column-Major: Benefits from spatial locality in column-wise data
access patterns.

Anish Kumar Yadav Understanding of Arrays July 11, 2024 16 / 20


Applications in Array Access Patterns

Matrix Operations: Optimizing performance based on access patterns.


Image Processing: Efficient manipulation of pixel data.
Scientific Computing: Handling large datasets effectively.

Anish Kumar Yadav Understanding of Arrays July 11, 2024 17 / 20


Real-World Example: Image Processing in C

Description: Manipulating an image represented as a 2D array.


Optimization: Choosing row-major or column-major order based on
processing requirements.
Example: Calculating pixel location for filters and transformations.

Anish Kumar Yadav Understanding of Arrays July 11, 2024 18 / 20


Conclusion on Multi-Dimensional Arrays in C

Summary: Understanding row-major and column-major order


enhances efficiency in array operations.
Implementation: Implementing optimized access patterns based on
application needs.

Anish Kumar Yadav Understanding of Arrays July 11, 2024 19 / 20


Q&A

Addressing questions on array access strategies.

Anish Kumar Yadav Understanding of Arrays July 11, 2024 20 / 20

You might also like