C++ Program to Delete an Element from Array

This is a C++ program to delete an element from array.

The user input an element to delete, the element is then searched in the array, if it is found it is deleted and new array is displayed. If the element is not present in the array then a not found message is displayed.

#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

 

9 thoughts on “C++ Program to Delete an Element from Array”

    1. ============================================================
      ============================================================

      //\\ ||===| ||===| ||===|| 44 \\ // ||===)
      /__\\ ||_____| ||_____| || || 4 4 \\ // || )
      // \\ || \\ || \\ ||ZERO|| 444444 // \\ || )
      // \\ || \\ || \\ ||===|| 4 // \\ ||===)

      ============================================================
      ============================================================
      This code is written for advance (c 11 and higher versions) versions of c++.
      TURBO c++ Is outdated a long time ago, So if you want to run the above code in your turbo than change :–
      #include —–>>> #include
      also remove this line “using namespace std;”

      OR Use another compiler such as CodeBlocks .

  1. q1.a function based menu driven program to do the following.options are
    a.count no of two lettered and three lettered words in a line
    b.count the frquency of a specified word in a line

Leave a Comment

Your email address will not be published. Required fields are marked *