Delete item in array in C++ by R4R Team

We want to delete item in Array:

Example-
Array is [1,2,3,4,5]
After deletion of index 2
Array will [1,2,4,5]

program:

#include< iostream.h>
int main()
{
int a[20],n,i,location;
cout<<"Enter number of elements in arary"<< endl;
cin>>n;
cout<<"Enter "<< n<<" number in array"<< endl;
for(i=0;i< n;i++)
cin>>a[i];
cout<<"Array is"<< endl;
for(i=0;i< n;i++)
cout<< a[i]<<" ";
cout<<"Enter the location for deletion";
cin>>location;
for(i=location-1;i< n-1;i++)
a[i]=a[i+1];
cout<<"Array after deletion is "<< endl;
for(i=0;i< n-1;i++)
cout<< a[i]< }


output-

Enter number of elements in array
6
Enter 6 number in array
1 2 3 4 5 6
Array is
1 2 3 4 5 6
Enter the location for deletion
2
Arary after deletion is
1 3 4 5 6

-In this program, Firstly we take an input in array, then we ask for in which location you want delete.
-So we just copy element after that index at previous index.
-So when we traverse again the array, it will not display that value which we want to delete.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!