Explore
ProGrad Programs
Placement Prep
TCS Codevita
Live Placement Training
Webinars
About Us

Edit
Reply




Edit

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

Published on 11 Mar 2020

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 an element in an array


C
C++
Java
Python 3

Output
5 (size of the array)
1 (array elements)
2
3
4
5
4 (Position)
10 (Element to be inserted)
Array after insertion is : 1 2 3 10 4 5


Program to insert an element in an array using pointers


// Solution using C pointers
#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;
}


Program to delete an element in an array


C
C++
Java
Python 3

Output
5 (size of the array)
1 (array elements)
2
3
4
5
4 (Position to be deleted)
Array after deletion is : 1 2 3 5


Program to delete an element in an array using pointers


// Solution using C pointers
#include<stdio.h>
#include<stdlib.h>
void delete(int n,int *a,int l);
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;
}
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”);
}
}


Program to search an element in an array


C
C++
Java
Python 3

Output
5 (size of the array)
1 (array elements)
2
3
4
5
5 (Element to be searched)
Element found


Program to search an element in an array using pointers


// Using Pointers, the solution is given below
#include<stdio.h>
int i,l;
int search(int ,int *,int);
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;
}
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”,m);

}
else
{
printf(“%d is not present in the array”,m);
}
}


Recommended Programs







If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in