Deleting an element in an array.

madhusudan · June 21, 2024
/*Write a C++ program to delete an element from array at specified position.*/
#include<iostream>
using namespace std;
void Delete(int arr[],int size,int position)
{
	int i;
	for(i=position;i<size;i++)
	{
		arr[i]=arr[i+1];
	}
	cout<<"Array elements After Deletion : ";
	for(i=0;i<size;i++)
	{
		cout<<arr[i]<<" ";
	}
}
int main()
{
	int size;
	// cout<<"Input the size of the array : ";
	cin>>size;
	int position;
	int arr[size];
	// cout<<"Input the array elements : ";
	for(int i=0;i<size;i++)
	{
		cin>>arr[i];	
	}
    size--;
	cout<<"Original Array Before Deletion : ";
	for(int i=0;i<size;i++)
	{
		cout<<arr[i]<<"  ";
	}
	// cout<<endl<<"Positon of element to delete : ";
	cin>>position;
	Delete(arr,size,position);
	return 0;
}
Output

Comments

Please sign up or log in to contribute to the discussion.