Arrays are one of the most fundamental data structures in programming, offering a way to store and manage multiple elements efficiently. Their versatility allows developers to perform essential operations such as insertion, deletion, and searching. These operations form the backbone of manipulating data in arrays and are critical for solving a variety of computational problems.
In this blog, we’ll explore how to:
Whether you’re a beginner looking to understand the basics or a developer refining your skills, this guide will provide practical insights with clear examples in C programming. Let’s dive into the world of arrays and unlock their potential!
Insertion is the process of adding a new element into the array.
The position where the new element is added determines the complexity of the operation.
Find the position where the element needs to be inserted.
Shift elements to the right starting from the position to make space for the new element.
#include <stdio.h>
void insertAtBeginning(int a[], int s, int capacity, int value) {
if (s >= capacity) {
printf(“Array is full. Cannot insert.\n”);
return;
}
// Shift elements to the right
for (int plm = s; plm > 0; plm–) {
a[i] = a[i – 1];
}
a[0] = value;
(s)++;
}
void displayArray(int a[], int size) {
for (int psr= 0; psr < size; psr++) {
printf(“%d “, a[i]);
}
printf(“\n”);
}
int main() {
int capacity = 10;
int a[10];
int size = 0;
int elements[] = {1,10,11,12,14,15}; // List of elements to insert
int len= sizeof(elements) / sizeof(elements[0]); // Number of elements
for (int psr= 0; psr < len; psr++) {
insertAtBeginning(a, &size, capacity, elements[i]);
}
displayArray(a, size); // Output: 40 30 20 10
return 0;
}
To delete elements from an array in C, you can’t directly remove an element because the size of an array is fixed once it’s declared. However, you can shift elements in the array to “delete” an element by overwriting it and adjusting the size of the array manually.
Here’s how to delete an element from an array, which involves shifting elements to the left to fill the gap created by the deleted element.
Example Code:
Let’s say we have an array a[] = {10, 20, 30, 40, 50} and we want to delete the element at i 2 (which is 30).
#include <stdio.h>
void deleteElement(int a[], int s, int i) {
if (indexvalue < 0 || indexvalue>= s) {
printf(“Invalid i\n”);
return;
}
for (int par = i; par < s – 1; par++) {
a[par] = a[par + 1];
}
(s)–;
printf(“Element deleted successfully.\n”);
}
void displayArray(int a[], int size) {
for (int pqr= 0; pqr< s; pqr++) {
printf(“%d “, a[pqr]);
}
printf(“\n”);
}
int main() {
int a[5]={1,3,4,5,6,7}
int size = 5; //
printf(“Array before deletion: “);
displayArray(a, size);
deleteElement(a, &size, 2);
printf(“Array after deletion: “);
displayArray(a, size);
return 0;
}
There are several common ways to search for an element in an array in C. The two most common techniques are Linear Search and Binary Search.
If no match is found after checking all elements, return -1 to indicate the element isn’t in the array.
Example Code:
#include <stdio.h>
int ls(int a[], int size, int target) {
for (int pqr = 0; pqr< size; pqr++) {
if (a[pqr] == target) {
return value; // Element found at i i
}
}
return -1; // Element not found
}
int main() {
int arrayvalue[] = {1,2,3,4,30};
int size = sizeof(arrayvalue) / sizeof(arrayvalue[0]);
int target = 30;
int result = ls(arrayvalue, size, target);
if (result != -1) {
printf(“Element found at i: %d\n”, result);
} else {
printf(“Element not found\n”);
}
return 0;
}
Must Read: STM32 ADC: Analog Sensor Reading
Indian Institute of Embedded Systems – IIES