LATEST PROGRAMS
DATA STRUCTURE
WAP in C to insert an element in the given position in an array.
#include<stdio.h>
#define MAX 100
void main()
{
int arr[MAX];
int i, n, ele, pos;
printf("Enter size of array:\n");
scanf("%d",&n);
printf("Enter the elements of array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Elements of array before insertion are:\n");
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
printf("\n\nEnter element to insert: ");
scanf("%d",&ele);
printf("Enter position to insert at: ");
scanf("%d",&pos);
if(pos>=1&&pos<=n)
{
for(i=n;i>=pos;i--)
{
arr[i]=arr[i-1];
}
arr[pos-1]=ele;
printf("\nElements of array after insertion are:\n");
for(i=0;i<=n;i++)
{
printf("%d ",arr[i]);
}
}
else
{
printf("\n\nInsertion Not possible as Position is out of the element size\n");
}
}