Insertion ,Deletion and searching in array in C


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


Operations of an array | Program to insert, delete and search an element in an array is discussed here. Given an array, the array operations like insert, delete and search an element in an array are performed and the result is displayed
If the position where the element to be inserted is greater than the size of an array display “Invalid Input”.
Input format:
  • Input consists of 3 integers and 1 array.
  • Input the size of the array.
  • Input the array elements.
  • Input the position where the element should be inserted.
  • Input the element to be inserted in that position.
Sample Input:
5  (size of the array)
1  (array elements)
2
3
4
5
4  (Position)
10 (Element to be inserted)
Sample  Output:
Array after insertion is:
1
2
3
10
4
5.

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

Insertion
  • Input the array elements, the position of the new element to be inserted and the new element.
  • Insert the new element at that position and shift the rest of the elements to right by one position.
Deletion
  • Input the array elements, the position of the new element to be inserted and the new element.
  • Delete the element and shift the rest of the elements to left by one position.
Search
  • Input the array elements, the element to be searched.
  • Traverse the array and check if the element is present in the array.
  • Display “Yes” if it is present in the array, else display “No”.
Program to insert, delete and search an element in an array is given below.

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

Insertion
  1. Input the array elements, the position of the new element to be inserted and the new element.
  2. Insert the new element at that position and shift the rest of the elements to right by one position.
Deletion
  • Input the array elements, the position of the new element to be inserted and the new element.
  • Delete the element and shift the rest of the elements to left by one position.
Searching
nput the array elements, the element to be searched.
  • Traverse the array and check if the element is present in the array.
  • Display “Yes” if it is present in the array, else display “No”.
Program to insert, delete and search an element in an array is given below.
 C program to insert an element in an array
/* C program to insert an element in an array */
#include
int main()
{
int n;
scanf(“%d”,&n);
int arr[n];
int i;
for(i = 0; i < n; i++)
{
scanf(“%d”,&arr[i]);
}
int pos;
scanf(“%d”,&pos);
int ele;
scanf(“%d”,&ele);
if(pos > n)
printf(“Invalid Input”);
else
{
for (i = n – 1; i >= pos – 1; i–)
arr[i+1] = arr[i];
arr[pos-1] = ele;
printf(“Array after insertion is:\n”);
for (i = 0; i <= n; i++)
printf(“%d\n”, arr[i]);
}
return 0;
}

Program to delete an element in an array 

/* C program to delete an element in an array */
#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf(“Enter the number of elements of the array : “);
scanf(“%d”, &n);
printf(“\nInput the array elements : “);
for (c = 0; c < n; c++)
scanf(“%d”, &array[c]);
printf(“\nEnter the position : “);
scanf(“%d”, &position);
if (position >= n+1)
printf(“\nDeletion not possible.\n”);
else
{
for (c = position – 1; c < n – 1; c++)
array[c] = array[c+1];
printf(“\nArray after deletion : “);
for (c = 0; c < n – 1; c++)
printf(“%d\n”, array[c]);
}
return 0;
}

/ C program to search an element in an array
#include <stdio.h>
int main()
{
int array[100], ele, c, n;
printf(“Enter the number of elements of the array : “);
scanf(“%d”, &n);
printf(“\nInput the array elements : “);
for (c = 0; c < n; c++)
scanf(“%d”, &array[c]);
printf(“\nEnter element : “);
scanf(“%d”, &ele);
for(c = 0; c < n ; c++)
{
if(array[c] == ele)
{
printf(“\nElement found\n”);
}
}
return 0;
}
Outputs:
Insert:
insert, delete and search an element in an array - insert
Delete:
insert, delete and search an element in an array - delete
Search:
insert, delete and search an element in an array

No comments:

Post a Comment

Featured Post

Core and Advance Python

                      Language Fundamentals