This C++ program deletes an element from an array. The size of the array is read as input into the variable n. Then the elements are read into the array a[] using a for loop. Then the position of the element to be deleted is read into the variable pos. The elements which lies after the element to be deleted is shifted one position backward, overwriting the element to be deleted. Then the new array is displayed to the output screen.
Program
//Delete element from array c++ program #include<iostream.h> #include<conio.h> void main() { int a[50],n,i,*p,pos; cout<<"Enter the value for n: "; cin>>n; cout<<"\nEnter the elements\n"; for(i=0;i<n;i++) { cin>>a[i]; } cout<<"\nEnter the position of element to delete: "; cin>>pos; p=&a[pos]; for(i=pos;i<n;i++) { *(p)=*(p+1); p++; } p=a; cout<<"\nArray after deletion\n\n"; for(i=0;i<n-1;i++) { cout<<*(p+i)<<"\t"; } getch(); }
C++ - delete an element from an array |
No comments:
Post a Comment