Program to delete an element from an array at specified position.



#include<stdio.h>
int main()
{
int arr[]={15,30,10,25,20,22,18,35,38,40};
int i, pos, temp, n=10;
printf("===============================\n");
printf("Delete an Element from an array\n");
printf("===============================\n");

//Displaying Positions and array elements
printf("Positions\n");
for(i=1;i<=n;i++)
{
printf("%d\t",i);
}

printf("Array Values\n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
printf("\n");


//Taking input from user for position number
printf("Enter position (upto 10): ");
scanf("%d",&pos);
pos-=1;

printf("\nAfter deleting %d from index arr[%d]\n",arr[pos],pos);
printf("The Array remains with following\n");
/* Process for deleting an element*/
for(i=pos; i<n; i++)
{
arr[i]=arr[i+1];
}
n--;

//Displaying New Array after deleting an element
printf("Positions\n");
for(i=1;i<=n;i++)
{
printf("%d\t",i);
}
printf("\nArray Values\n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
getch();
}

No comments

Post your comments

Powered by Blogger.