Monday, 7 April 2014

Data Structure C++ Code of Array Deletion of Elements

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,item,loc,j;

cout<<"\n------------ DELETE ELEMENT FROM ARRAY ------------ \n\n";

cout<<"Enter No. of Elements : ";
cin>>n;

cout<<"\nEnter Elements : \n";
for(i=0;i<n;i++)
{
cin>>a[i];
}

cout<<"\nEnter Location of Element, you want to Delete : ";
cin>>loc;


item=a[loc];

for(j=loc;j<n-1;j++)                  // "j" is initliazed by by "n-1" becasue in C++ language array subscript is started from 0 index number.     
{
a[j]=a[j+1];                         // Move Elemets Upward
}

n=n-1;                               // Reset the No. of Elemets in Array

cout<<"\nArray after Deletion : \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}

cout<<"\nElement "<<item<<" is deleted";
getch();
}

No comments:

Post a Comment