Deletion of element from array

http://install4install.com

Deleting  an  element means to remove element from the array.It is quite easy to delete the element from the end of array.On the controversial side,what is needed to delete the element from our  desired location  like somewhere in the middle of array .Deleting an element requires that all the subsequent elements must be moved one location up to keep the order of array.

The following program is to delete element from the end of array.

Program

#include <stdio.h>

int main()
{
   int arr[10] = {10,20,30,40,50};
   int pos;
   int i;
   printf("Enter the location where you wish to delete element\n");
   scanf("%d", &pos);
   if ( pos > 4 )
      printf("Deletion not possible.\n");
   else
   {
     for ( i = pos  ; i < 4 ; i++ )
     {
     a[i] = a[i + 1];
     }
     printf("Resultant array is\n");
     for( i = 0 ; i < 4 ; i++ )
     {
      printf("%d\t", arr[i]);
      }
   }  

return 0;
}
Run

Illustration of Program

Fig 1 : Elements of array before deletion.

Fig 1 : Elements of array before deletion.

Fig 2 : Working of for loops

Fig 2 : Working of for loops

Fig 3 :Elements after deletion of 30 at 2 position.

Fig 3 :Elements after deletion of 30 at 2 position.

 

 



Short URL: http://tinyit.cc/94100c
Author: Cusp2207 on March 21, 2014
Category: Algorithms, Computer Science, Data Structures
Tags:

Leave a Reply

Last articles