Search This Blog

Program to delete an element from an Array

Wap to delete an element from an Array

//source code:


#include
#include
int main()
{
int arr[100];
int k,i,size;
clrscr();
printf(" Enter  number of elements: ");
scanf("%d",&size);
printf("Enter array elements\n");
scanf("%d",&arr[i]);
printf(" Array entered is :\n");
printf(" %d ",arr[i]);


printf("\nEnter  the position(index) at which  you want to  delete element\n");
scanf("%d",&k);
if(k>=size)
{
printf("Index out of range");
exit(0);
}
while(k<=size)
{

arr[k]=arr[k+1];
k++;
}
printf(" New Array is:\n");
for (i=0;i
{
printf(" %d ",arr[i]);
}
getch();
}
Output:

  
Logic:
1.first enter the index at which you want to delete an element.
2.compare the index with the size of an array
like        
             if(k>=size)
if the index is greater than the size of an array then there is no element to delete,i.e.,it is invalid input,this exit the program.
3.Copy the elements from (k+1)th position to kth position until the end of array.
4.print the array.                    

No comments:

Post a Comment

leave a comment here