Delete an element from an array in C++
Delete an element from a single dimensional array using C++. The program just overwrite the position that we want to delete from the array. We are just moving all the element below that position to one position higher.
#include<iostream> #include<iomanip> using namespace std; void input(int x[], int n){ for(int i=0;i<n;i++) { cout<<"Enter "<<i<<" element :"; cin>>x[i]; } return; } void output(int x[], int n){ for(int i=0;i<n;i++) cout<<setw(6)<<x[i]; return; } void delete_element(int x[], int &m, int pos) { if(m<=0){ cout<<"Array Empty. Can't' DELETE element"; } else { for(int i=pos-1;i<m;i++) x[i]= x[i+1]; m = m-1; } return; } int main(){ int x[1000],m, pos; //input phase cout<<"Enter size of filled position :"; cin>>m; input(x,m); cout<<"\n Enter position that you want to delete :"; cin>>pos; //processing phase delete_element(x,m,pos); //output cout<<"\n\nArray after insertion \n"; output(x,m); }
The above code is implemented using Dev C++ on windows system. The output of the above program is as follows.
If you have any doubt/query related to above C++ program, Just send us your queries using comments.