Array Operations and Traversals
1. Introduction
An array is a collection of elements of the same type stored in contiguous memory locations.
Elements are accessed using indices starting from 0.
Example:
int arr[5] = {1, 2, 3, 4, 5};
2. Array Operations
a) Insertion
Definition: Add a new element at a specific position.
Example:
# include <stdio.h>
int main() {
int arr[6] = {1, 2, 3, 4, 5};
int n = 5;
int pos = 2, value = 10;
for(int i = n; i > pos; i--) {
arr[i] = arr[i-1];
}arr[pos] = value;
n++;
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
b) Deletion
Definition: Remove an element from a position.
Example:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 10, 3, 4};
int n = 5;
int pos = 2;
for(int i = pos; i < n-1; i++) {
arr[i] = arr[i+1];
}
n--;
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
c) Updation (Modification) Definition: Change an element at a specific index.
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
arr[2] = 10;
for (int i = 0; i < 5; i++)
printf("%d ", arr[i]);
return 0;
}
d) Searching Definition: Find the position of a specific element.
Example:
#include <stdio.h>
int main () {
int arr[5] = {1, 2, 10, 4, 5};
int key = 10;
for (int i = 0; i < 5; i++) {
if(arr[i] == key) {
printf("Element found at index %d", i);
break;
}
}
return 0;
}
Traversal
Definition: Access each element to display or perform operations.
Example:
for(int i=0; i<5; i++)
printf("%d ", arr[i]);
3. Types of Traversals
1. Linear Traversal: First to last element.
2. Reverse Traversal: Last to first element.
3. Conditional Traversal: Access elements meeting a condition (e.g., even numbers).
4. Applications
- Insertion/Deletion: Database record management.
- Searching: Retrieving specific data.
- Traversal: Displaying data, calculating sum, max, or min.
5. Key Points
- Arrays are fixed size in C.
- Indices start at 0.
- Insertion and deletion may require shifting elements.
- Useful for storing multiple values of the same type.
1. Linear Traversal (First to Last)
#include <stdio.h>
int main() {
int arr [5] = {1, 2, 3, 4, 5};
printf("Linear Traversal: ");
for (int i = 0; i < 5; i++) {
printf ("%d ", arr[i]);
}
return 0;
}
2. Reverse Traversal (Last to First)
#include <stdio.h>
int main () {
int arr [5] = {1, 2, 3, 4, 5};
printf ("Reverse Traversal: ");
for (int i = 4; i >= 0; i--) {
printf ("%d ", arr[i]);
}
return 0;
}
3. Conditional Traversal
#include <stdio.h>
int main() {
int arr[6] = {1, 2, 3, 4, 5, 6};
printf ("Even Numbers: ");
for (int i = 0; i < 6; i++) {
if(arr[i] % 2 == 0) {
printf ("%d ", arr[i]);
}
}
return 0;
}
1. Definition
A pointer is a variable that stores the address of another variable in
memory.
It “points” to the location of data rather than storing the data itself.
EXAMPLE:
#include <stdio.h>
int main() {
int a = 10, b = 20;
int *p1 = &a;
int *p2 = &b;
int temp;
// Swapping using pointers
temp = *p1;
*p1 = *p2;
*p2 = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Pointer Arithmetic:
1. Definition
Pointer Arithmetic: Performing operations on pointers to navigate
memory locations.
Common operations:
o p++ → move pointer to next element
o p-- → move pointer to previous element
o p + n → move pointer n elements forward
o p - n → move pointer n elements backward
EXAMPLE:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *p = arr; // points to first element
printf("First element: %d\n", *p); // 10
p++; // move to next element
printf("Second element: %d\n", *p); // 20
p += 2; // move two elements ahead
printf("Fourth element: %d\n", *p); // 40
return 0;
}
OUTPUT:
First element: 10
Second element: 20
Fourth element: 40
Rules
1. Arithmetic is only valid for pointers pointing to array elements.
2. Increment/decrement moves based on data type size:
o int* → moves 4 bytes (if int = 4 bytes)
o char* → moves 1 byte
3. Dereference *p to access the value at the pointer.
Pointers and Arrays
1. Definition
Arrays and pointers are closely [Link] name of an array acts as a
pointer to its first element. Pointer arithmetic can be used to access
array elements.
Key Points
arr[i] is equivalent to *(arr + i)
Array name arr = address of arr[0]
Useful in function arguments and dynamic memory.
EXAMPLE:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr; // pointer points to first element
for(int i = 0; i < 5; i++)
printf("%d ", *(p + i)); // access elements using pointer
return 0;
}
Pointers to Functions
1. Definition
A function pointer stores the address of a function.
It can be used to call the function indirectly.
Useful in callback functions, dynamic function calls, and event-driven
programming.
PROGRMA: REFER CALL BY REFRENCE
Dynamic Memory Allocation
Definition
Dynamic memory allocation (DMA): Allocating memory at runtime
instead of compile-time.
Allows creation of arrays or variables whose size is determined during
execution.
Managed using functions from <stdlib.h>: malloc(), calloc(), realloc(),
free().
2. Functions
a) malloc()
Allocates memory of specified size.
Memory is not initialized.
ptr = (int*)malloc(n * sizeof(int));
b) calloc()
Allocates memory for n elements, initialized to 0.
ptr = (int*)calloc(n, sizeof(int));
c) realloc()
Resizes previously allocated memory block.
ptr = (int*)realloc(ptr, new_size * sizeof(int));
d) free()
Frees allocated memory to avoid memory leaks.
free(ptr);
EXAMPLE:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int *arr = (int*)malloc(n * sizeof(int)); // allocate memory
for(int i = 0; i < n; i++)
arr[i] = i + 1; // store values
printf("Array elements: ");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
free(arr); // release memory
return 0;
}