Program to delete values from the array in CPP (C plus plus)

Program to delete values from the array in CPP (C plus plus)

In this tutorial, we will learn about Program to delete values from the array in CPP (C plus plus) .

#include<iostream>
 
using namespace std;
 
int main()
{
    int array[50];
	int d;
	int no;
	int i;
	int j;
	int k[50];
	
    cout<<"Please enter the number of values of Array you want to create: ";
    cin>>no;
    
    cout<<endl<<"Please enter values in Array: "<<endl;
    
    for(i=0;i<no;++i)
        cin>>array[i];
        
    cout<<endl<<"Please enter the value you want to delete: ";
    cin>>d;
    
    for(i=0,j=0;i<no;++i)
    {
        if(array[i]!=d)
            k[j++]=array[i];
    }
 
    if(j==no)
    {
        cout<<endl<<"Sorry! Entered value not found in the Array";
        exit(0);
    }
    
    else
    {
        cout<<endl<<"Updated Array is ";
        for(i=0;i<j;i++)
            cout<<k[i]<<" ";
    }
 
    return 0;
}

Output:

Program to delete values from the array in CPP (C plus plus)
Program to delete values from the array in CPP (C plus plus)

Delete array values Using Array in C++

Let see the program of “Deleting the array values Using Array in C++”.

#include<iostream>
using namespace std;
int main()
{
	int array[5];
	int new_values[4];
	int place;
	
	cout<<"please enter the 5 values "<<endl;
	for(int i=0; i<5; i++)
	{
		cin>>array[i];
	}
	cout<<"The values stored in array are: ";
	for(int i=0; i<5; i++)
	{
		cout<<array[i]<<" ";
	}
	cout<<endl<<"Please enter the Place of value to delete"<<endl;
	cin>>place;
	if(place>5)
	{
		cout<<"the place you entered is not in array"<<endl;
	}
	else
	{
		cout<<"the value has been deleted: "<<endl;
		int i;
		place--;
		for(i=0; i<place; i++)
			{
			new_values[i]=array[i];	
			}
		for(i=place; i<=4; i++)
		{
			new_values[i]=array[i+1];
		}
		cout<<"New values are : ";
		for(int i=0; i<4; i++)
		{
			cout<<new_values[i]<<" ";
		}
	}
}

Output

Delete array values Using Array in C++
Figure: Delete array values Using Array in C++

Delete array values Using Switch Statement in C++

Let see the program of “Deleting the array values Using Switch Statement in C++”.

#include<iostream>
using namespace std;
int main()
{
	int array[5];
	int place;
	int val;
	
	cout<<"please enter 5 values one by one"<<endl;
	for(int i=0; i<5; i++)
		{
			cin>>array[i];
		}
		
	cout<<"These are the values Entered by you: ";
	for(int i=0; i<5; i++)
		{
			cout<<array[i]<<" ";
		}
		
	cout<<endl<<"please type the place of value you want to delete:"<<endl;
	cin>>place;
	
	cout<<"The value on given location has been deleted"<<endl;
	if(place>5)
		{
			val=1;
		}
	switch(val)
		{
			case 1:
				{
					cout<<"The place you entered is not found:"<<endl;
					break;
				}
			default:
				{
					place--;
					for(int i=place; i<5; i++)
						{
							array[i]=array[i+1];
						}
				}
		}
	cout<<"The new values are: ";
	for(int i=0; i<4; i++)
		{
			cout<<array[i]<<" ";
		}
		
	
}

Delete array values Using Do While Loop in C++

Let see the program of “Deleting the array values Using Do While Loop in C++”.

#include<iostream>
using namespace std;
int main()
{
	int array[5];
	int place;
	
	cout<<"please enter the 5 values "<<endl;
	int i=0;
	do
	{
		cin>>array[i];
		i++;
	}
	while(i<5);
	cout<<"The values stored in array are: ";
	i=0;
	do
	{
		cout<<array[i]<<" ";
		i++;
	}
	while(i<5);
	cout<<endl<<"Please enter the Place of value to delete"<<endl;
	cin>>place;
	if(place>5)
	{
		cout<<"the place you entered is not in array"<<endl;
	}
	else
	{
		int i;
		place--;
		i=place;
		cout<<"the value has been deleted: "<<endl;
		do
		{
			array[i]=array[i+1];
			i++;
		}
		while(i<=4);
		cout<<"New values are : ";
		i=0;
		do
		{
			cout<<array[i]<<" ";
			i++;
		}
		while(i<4);
	}
}

Delete array values Using For Loop in C++

Let see the program of “Deleting the array values Using For Loop in C++”.

#include<iostream>
using namespace std;
int main()
{
	int array[5];
	int place;
	
	cout<<"please enter the 5 values "<<endl;
	for(int i=0; i<5; i++)
	{
		cin>>array[i];
	}
	cout<<"The values stored in array are: ";
	for(int i=0; i<5; i++)
	{
		cout<<array[i]<<" ";
	}
	
	for(int p=0; p<1;)
	{
	cout<<endl<<"Please enter the Place of value to delete"<<endl;
	cin>>place;
	
	if(place>5)
	{
		cout<<"the place you entered is not in array"<<endl;
	}
	else
	{
		cout<<"the value has been deleted: "<<endl;
		int i;
		place--;
		for(i=place; i<=4; i++)
		{
			array[i]=array[i+1];
		}
		cout<<"New values are : ";
		for(int i=0; i<4; i++)
		{
			cout<<array[i]<<" ";
		}
		p++;
	}
	}	
}

Delete array values by Using User Define Function in C++

Let see the program of “Deleting the array values by Using the user define functions in C++”.

#include<iostream>
using namespace std;
int deletion(int[],int&,int&);
int main()
{
	int array[5];
	int place;
	int p=0;
	
	cout<<"please enter the 5 values "<<endl;
	for(int i=0; i<5; i++)
	{
		cin>>array[i];
	}
	cout<<"The values stored in array are: ";
	for(int i=0; i<5; i++)
	{
		cout<<array[i]<<" ";
	}
	cout<<endl<<"Please enter the Place of value to delete"<<endl;
	cin>>place;
	cout<<"the value has been deleted: "<<endl;
	deletion(array,place,p);
	for(int i=0; i<4; i++)
		{
			cout<<array[i]<<" ";
		}
}

int deletion(int array[5],int &place,int &p)
{
	if(place>5)
	{
		cout<<"the place you entered is not in array"<<endl;
	}
	else
	{
		place--;
		for(p=place; p<=4; p++)
		{
			array[p]=array[p+1];
		}
		cout<<"New values are: ";
		
	}
}

Delete array values by using If Else Statement in C++

Let see the program of “Deleting the array values by Using the If Else Statement in C++”.

#include<iostream>
using namespace std;
int main()
{
	int array[5];
	int place;
	
	cout<<"please enter the 5 values "<<endl;
	int i=0;
	while(i<5)
	{
		cin>>array[i];

		i++;
	}
	cout<<"The values stored in array are: ";
	i=0;
	while(i<5)
	{
		cout<<array[i]<<" ";
		i++;
	}
	cout<<endl<<"Please enter the Place of value to delete"<<endl;
	cin>>place;
	if(place>5)
	{
		cout<<"the place you entered is not in array"<<endl;
	}
	else
	{
		int i;
		place--;
		i=place;
		cout<<"the value has been deleted: "<<endl;
		while(i<=4)
		{
			array[i]=array[i+1];
			i++;
		}
		cout<<"New values are : ";
		i=0;
		while(i<4)
		{
			cout<<array[i]<<" ";
			i++;
		}
	}
}

Delete array values by using Nested If Else Statement in C++

Let see the program of “Deleting the array values by Using the Nested If Else Statement in C++”.

#include<iostream>
using namespace std;
int main()
{
	int array[5];
	int place;
	int val;
	
	cout<<"please enter the 5 values "<<endl;
	int i=0;
	while(i<5)
	{
		cin>>array[i];
		i++;
	}
	cout<<"The values stored in array are: ";
	i=0;
	while(i<5)
	{
		cout<<array[i]<<" ";
		i++;
	}
	cout<<endl<<"Please enter the Place of value to delete"<<endl;
	cin>>place;
	cout<<"Are you sure you want to delete.?"<<endl<<"type '1' to delete"<<endl;
	cin>>val;
	if(val==1)
		{
	if(place>5)
	{
		cout<<"the place you entered is not in array"<<endl;
	}
	else
	{
		int i;
		place--;
		i=place;
		cout<<"the value has been deleted: "<<endl;
		while(i<=4)
		{
			array[i]=array[i+1];
			i++;
		}
	}
		cout<<"New values are : ";
		i=0;
		while(i<4)
		{
			cout<<array[i]<<" ";
			i++;
		}
	}
	else
	{
		cout<<"nothing deleted.."<<endl;
	}
}

Delete array values by using the While Loop in C++

Let see the program of “Deleting the array values by Using the While Loop in C++”.

#include<iostream>
using namespace std;
int main()
{
	int array[5];
	int place;
	
	cout<<"please enter the 5 values "<<endl;
	int i=0;
	while(i<5)
	{
		cin>>array[i];
		i++;
	}
	cout<<"The values stored in array are: ";
	i=0;
	while(i<5)
	{
		cout<<array[i]<<" ";
		i++;
	}
	cout<<endl<<"Please enter the Place of value to delete"<<endl;
	cin>>place;
	if(place>5)
	{
		cout<<"the place you entered is not in array"<<endl;
	}
	else
	{
		int i;
		place--;
		i=place;
		cout<<"the value has been deleted: "<<endl;
		while(i<=4)
		{
			array[i]=array[i+1];
			i++;
		}
		cout<<"New values are : ";
		i=0;
		while(i<4)
		{
			cout<<array[i]<<" ";
			i++;
		}
	}
}

 

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.