C++ Program to Delete an Element from Array: The user inputs an element to delete, the element is then searched in the array, if it is found it is deleted and the new array is displayed. you have to first ask the user to enter the array size then ask to enter the array elements,
C++ Program to Delete an Element from Array
#include<iostream>
#include<process.h>
using namespace std;
int main()
{
int a[50],x,n,i,j,b[50];
cout<<"How many elements of Array you want to create?";
cin>>n;
cout<<"\nEnter elements of Array\n";
for(i=0;i<n;++i)
cin>>a[i];
cout<<"\nEnter element to delete:";
cin>>x;
for(i=0,j=0;i<n;++i)
{
if(a[i]!=x)
b[j++]=a[i];
}
if(j==n)
{
cout<<"\nSoory!!!Element is not in the Array";
exit(0);
}
else
{
cout<<"\nNew Array is ";
for(i=0;i<j;i++)
cout<<b[i]<<" ";
}
return 0;
}
Output of Program
How many elements of Array you want to create?5
Enter elements of Array
5
5
6
8
9
Enter element to delete:5
New Array is 6 8 9