Program to insert, delete and search an element in an array

Program to insert, delete and search an element in an array

How to Perform Insert, Delete, and Search Operations in an Array | C Programming Tutorial

In this article, we will cover the basic array operations: insert, delete, and search. These operations allow us to modify and interact with arrays in C programming. Learn how to insert an element at any position, delete an element, and search for an element within an array using pointers. We will walk through examples, provide code snippets, and explain how each operation works.

Table of Contents:


Inserting an Element in an Array

Inserting an element into an array is a common operation where you add an element at a specific position. If the position exceeds the size of the array, we display an “Invalid Input” message. Here’s how to do it.

Example Input:

  • Size of the Array: 5
  • Array Elements: 1, 2, 3, 4, 5
  • Position to Insert: 4
  • Element to Insert: 10

Output:

Array after insertion is:

CopyEdit1
2
3
10
4
5

C Code for Insertion:

#include <stdio.h>
#include <stdlib.h>

void insert(int n1, int *a, int l, int e) {
int i;
printf("Array after insertion is:\n");
for(i = 0; i < l - 1; i++) {
printf("%d\n", *(a + i));
}
printf("%d\n", e);
for(i = l - 1; i < n1; i++) {
printf("%d\n", *(a + i));
}
}

int main() {
int *a, n1, i, l, e;
scanf("%d", &n1);
a = (int*)malloc(n1 * sizeof(int));
for(i = 0; i < n1; i++) {
scanf("%d", a + i);
}
scanf("%d", &l);
if(l <= n1) {
scanf("%d", &e);
insert(n1, a, l, e);
} else {
printf("Invalid Input");
}
return 0;
}

Deleting an Element from an Array

When deleting an element from an array, you need to shift the remaining elements to the left after removal. If the position exceeds the array size, an “Invalid Input” message will be displayed.

Example Input:

  • Size of the Array: 5
  • Array Elements: 1, 2, 3, 4, 5
  • Position to Delete: 3

Output:

Array after deletion is:

CopyEdit1
2
4
5

C Code for Deletion:

#include <stdio.h>
#include <stdlib.h>

void delete(int n, int *a, int l) {
int i, j;
if(l <= n) {
for(i = l - 1; i < n; i++) {
j = i + 1;
*(a + i) = *(a + j);
}
printf("Array after deletion is:\n");
for(i = 0; i < n - 1; i++) {
printf("%d\n", *(a + i));
}
} else {
printf("Invalid Input");
}
}

int main() {
int *a, n, i, l;
scanf("%d", &n);
a = (int*)malloc(sizeof(int) * n);
for(i = 0; i < n; i++) {
scanf("%d", (a + i));
}
scanf("%d", &l);
delete(n, a, l);
return 0;
}

Searching for an Element in an Array

The search operation checks if a given element exists in the array. If it does, it displays a message confirming the presence; otherwise, it informs the user that the element is absent.

Example Input:

  • Size of the Array: 5
  • Array Elements: 1, 2, 3, 4, 5
  • Element to Search: 3

Output:

cCopyEdit3 is present in the array

C Code for Search:

#include <stdio.h>

int i, l;

int search(int n, int *a, int m) {
for(i = 0; i < n; i++) {
if(m == a[i]) {
l = 1;
break;
}
}
if(l == 1) {
printf("%d is present in the array\n", m);
} else {
printf("%d is not present in the array\n", m);
}
}

int main() {
int n, m;
scanf("%d", &n);
int a[n];
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
scanf("%d", &m);
search(n, a, m);
return 0;
}

Frequently asked questions in interviews

1. Reverse a String

Q: Write a C program to reverse a given string without using the strrev() function.

#include <stdio.h>
#include <string.h>

void reverseString(char str[]) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}

int main() {
char str[] = "Hello";
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
}

Explanation:

  • The function swaps characters from the beginning and end, moving towards the center.

2. Check if a Number is Prime

Q: Write a C program to check whether a given number is prime or not.

#include <stdio.h>

int isPrime(int num) {
if (num < 2) return 0;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return 0;
}
return 1;
}

int main() {
int num = 29;
if (isPrime(num))
printf("%d is a prime number\n", num);
else
printf("%d is not a prime number\n", num);
return 0;
}

Explanation:

  • A number is prime if it has no divisors other than 1 and itself.
  • The loop runs until sqrt(num) to optimize performance.

3. Swap Two Variables Without Using a Third Variable

Q: Write a program to swap two numbers without using a third variable.

#include <stdio.h>

int main() {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}

Explanation:

  • a is set to a + b, then b gets a - b, and finally, a gets a - b.

4. Find Factorial Using Recursion

Q: Write a program to find the factorial of a number using recursion.

#include <stdio.h>

long long factorial(int n) {
return (n == 0 || n == 1) ? 1 : n * factorial(n - 1);
}

int main() {
int num = 5;
printf("Factorial of %d is %lld\n", num, factorial(num));
return 0;
}

Explanation:

  • The function calls itself to calculate n! = n * (n-1)!.

5. Find the Largest Element in an Array

Q: Write a program to find the largest element in an array.

#include <stdio.h>

int findLargest(int arr[], int size) {
int max = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}

int main() {
int arr[] = {10, 20, 30, 25, 40};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Largest element: %d\n", findLargest(arr, size));
return 0;
}

Explanation:

  • The program iterates through the array to find the maximum element.

Conclusion

Array operations like insertion, deletion, and searching are crucial for manipulating data structures in programming. In this tutorial, we walked through practical examples with detailed explanations and code snippets. By understanding these basic operations, you can easily handle arrays in C programming and build more complex data structures.

Program to insert, delete and search an element in an array


c