C Array Operations: Insertion, Deletion, Search, and Sort
Array Insertion
This program demonstrates how to insert a new number into an array at a specified position.
#include <stdio.h>
void insert(int a[], int len, int pos, int num);
int main() {
int a[10];
int len, pos, num;
printf("Enter the number of elements you want in an array\n");
scanf("%d", &len);
printf("Enter %d integers\n", len);
for (int i = 0; i < len; i++)
scanf("%d", &a[i]);
printf("Enter the number you want to insert in an array\n");
scanf("%d", &num);
printf("Enter the position (1 to %d) for insertion\n", len);
scanf("%d", &pos);
pos--;
insert(a, len, pos, num);
printf("Array after insertion\n");
for (int i = 0; i <= len; i++)
printf("%d ", a[i]);
return 0;
}
void insert(int a[], int len, int pos, int num) {
for (int i = len; i > pos; i--) {
a[i] = a[i - 1];
}
a[pos] = num;
}
Array Deletion
This program demonstrates how to delete a number from an array at a given position.
#include <stdio.h>
int main() {
int a[10], len, pos, i;
printf("Enter the length of the array (max 10)\n");
scanf("%d", &len);
if (len <= 0 || len > 10) {
printf("Invalid Length! It should be between 1 to 10\n");
return 1;
}
printf("Enter the elements of the array:\n");
for (i = 0; i < len; i++) {
scanf("%d", &a[i]);
}
printf("Enter the position for deletion (1 to %d): ", len);
scanf("%d", &pos);
if (pos < 1 || pos > len) {
printf("Position exceeds the length!\n");
return 1;
}
for (i = pos - 1; i < len - 1; i++) {
a[i] = a[i + 1];
}
len--;
printf("Array after deletion:\n");
for (i = 0; i < len; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Array Search
This program demonstrates how to search for an item in an array and find its index.
#include <stdio.h>
int main() {
int a[10], i, num, len, found = 0;
printf("Enter the length of the array (max 10)\n");
scanf("%d", &len);
if (len <= 0 || len > 10) {
printf("Invalid Length! It should be between 1 to 10\n");
return 1;
}
printf("Enter the elements in the array:\n");
for (i = 0; i < len; i++) {
scanf("%d", &a[i]);
}
printf("Enter the element to be searched in the array:\n");
scanf("%d", &num);
for (i = 0; i < len; i++) {
if (a[i] == num) {
printf("%d is found at index %d\n", num, i);
found = 1;
break;
}
}
if (!found) {
printf("Entered element is not found\n");
}
return 0;
}
Bubble Sort
This program demonstrates how to sort elements in an array in ascending order using the bubble sort algorithm.
#include <stdio.h>
int main() {
int array[10];
int i, j, len, temp;
printf("Enter the length of the array (max 10)\n");
scanf("%d", &len);
if (len <= 0 || len > 10) {
printf("Invalid Length! It should be between 1 to 10\n");
return 1;
}
printf("Enter the elements in the array one by one:\n");
for (i = 0; i < len; i++) {
scanf("%d", &array[i]);
}
printf("Elements in the array are:\n");
for (i = 0; i < len; i++) {
printf("%d\n", array[i]);
}
// Bubble sort
for (i = 0; i < len - 1; i++) {
for (j = 0; j < len - i - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp; // Corrected swapping logic
}
}
}
printf("Sorted Array is:\n");
for (i = 0; i < len; i++) {
printf("%d\n", array[i]);
}
return 0;
}