Delete an Element from Array in C++
Advertisements
C++ Program to Delete an element from Array
Delete an element in an array from specific position
#include<iostream.h> #include<conio.h> void main() { int i,a[5],no,pos; clrscr(); cout<<"Enter Data in Array: "; for(i=0;i<5;i++) { cin>>a[i]; } cout<<"\n\nStored Data in Array: "; for(i=0;i<5;i++) { cout<<a[i]; } cout<<"\n\nEnter poss. of Element to Delete: "; cin>>pos; if(pos>5) { cout<<"\n\nThis value is out of range: "; } else { --pos; for(i=pos;i<=4;i++) { a[i]=a[i+1]; } cout<<"\n\nNew Data in Array: "; for(i=0;i<4;i++) { cout<<a[i]; } } getch(); }
Output
Enter Data in Array: 10 20 30 40 50 Stored Data in Array: 10 20 30 40 50 Enter poss. of Element to Delete: 2 New data in Array: 10 20 40 50
Google Advertisment