Social Icons

Monday, 17 February 2014

Program to delete an element from an Array

#include<iostream.h>
#include<process.h>
#include<conio.h>

void main()
{
clrscr();
int a[50],x,n,i,j,b[50];
cout<<"\n How many elements you want to enter : ";
cin>>n;
cout<<"\n Enter elements \n";
for(i=0;i<n;++i)
cin>>a[i];
cout<<"\n\tEnter 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<<"\n Element is not in the Array";
exit(0);
}
else
{
cout<<"\n New Array is : ";
for(i=0;i<j;i++)
cout<<b[i]<<" ";
}
getch();
}


                                                       Output

No comments:

Post a Comment