Arrays are a fundamental data structure in programming, and mastering their operations is essential for any beginner. This blog will guide you through the basic operations of insertion, deletion, and traversal in C using a simple example. Let’s dive in!
1. Inserting an Element into an Array
Insertion is the process of adding a new element at a specified position in the array. Here’s how we can achieve this in C.
Example Code
#include <stdio.h>
void insert(int arr[], int *n, int pos, int value) {
for (int i = *n; i > pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos] = value;
(*n)++;
}
int main() {
int arr[10] = {1, 2, 4, 5};
int n = 4;
int pos = 2;
int value = 3;
insert(arr, &n, pos, value);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Explanation
- Header File Inclusion
#include <stdio.h>
This line includes the standard input-output header file necessary for using printf and other standard I/O functions.
- Insert Function Definition
void insert(int arr[], int *n, int pos, int value) {
for (int i = *n; i > pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos] = value;
(*n)++;
}
- The
insertfunction takes four parameters:arr[]: The array where the new value will be inserted.*n: A pointer to an integer representing the current size of the array.pos: The position at which the new value should be inserted.value: The value to be inserted into the array.
- Inside the function:
- A loop shifts elements to the right to make space for the new value.
- The new value is inserted at the specified position.
- The size of the array is incremented.
- Main Function
int main() {
int arr[10] = {1, 2, 4, 5};
int n = 4;
int pos = 2;
int value = 3;
insert(arr, &n, pos, value);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
- An array
arrof size 10 is initialized with four elements:1, 2, 4, 5. - Variables
n,pos, andvalueare initialized to 4, 2, and 3, respectively. - The
insertfunction is called to insert the value3at index2of the array. - A loop prints each element of the array after insertion.
2. Deleting an Element from an Array
Deletion is the process of removing an element from a specified position in the array.
Example Code
#include <stdio.h>
void delete(int arr[], int *n, int pos) {
for (int i = pos; i < *n - 1; i++) {
arr[i] = arr[i + 1];
}
(*n)--;
}
int main() {
int arr[10] = {1, 2, 3, 4, 5};
int n = 5;
int pos = 2;
delete(arr, &n, pos);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Explanation
- Delete Function Definition
void delete(int arr[], int *n, int pos) {
for (int i = pos; i < *n - 1; i++) {
arr[i] = arr[i + 1];
}
(*n)--;
}
- The
deletefunction takes three parameters:arr[]: The array from which the value will be deleted.*n: A pointer to an integer representing the current size of the array.pos: The position of the value to be deleted.
- Inside the function:
- A loop shifts elements to the left to fill the gap left by the deleted value.
- The size of the array is decremented.
- Main Function
int main() {
int arr[10] = {1, 2, 3, 4, 5};
int n = 5;
int pos = 2;
delete(arr, &n, pos);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
- An array
arrof size 10 is initialized with five elements:1, 2, 3, 4, 5. - Variables
nandposare initialized to 5 and 2, respectively. - The
deletefunction is called to delete the value at index2of the array. - A loop prints each element of the array after deletion.
3. Traversing an Array
Traversal is the process of accessing each element of the array exactly once.
Example Code
#include <stdio.h>
void traverse(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int n = 5;
traverse(arr, n);
return 0;
}
Explanation
- Traverse Function Definition
void traverse(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
- The
traversefunction takes two parameters:arr[]: The array to be traversed.n: The size of the array.
- Inside the function:
- A loop prints each element of the array.
- Main Function
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int n = 5;
traverse(arr, n);
return 0;
}
- An array
arrof size 5 is initialized with five elements:1, 2, 3, 4, 5. - Variable
nis initialized to 5. - The
traversefunction is called to print each element of the array.
Conclusion
By understanding these basic operations, you can efficiently manipulate arrays in C. Practice these operations with different examples to gain confidence. Happy coding!