C++ Program to Delete an Element from Array

This is a C++ program to erase a component from the exhibit.

The client input a component to erase, the component is then looked in the cluster, on the off chance that it is discovered it is erased and the new exhibit is shown. On the off chance that the component is absent in the exhibit, at that point a not discovered message is shown.

#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

How many elements of Array you want to create?5

Enter elements of Array
14 8 3 6 9

Enter element to delete:6

New Array is 14 8 3 9

Leave a Comment

error: Alert: Content is protected!!