Sie sind auf Seite 1von 2

#include<stdio.

h>
main()
{
int delpos(int,int,int[]);
int a[30],i,j,n,pos,choice,ele,flag=0;
char ch;
do
{
system("cls");
printf("\nProgram to delete an element from an
array");
printf("\n\nEnter the number of elements in the
array:");
scanf("%d",&n);
printf("\n Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n 1.Deletion by position\n 2.Deletion by
element");
printf("\n Enter the choice:");
scanf("%d",&choice);
if(choice==1)
{
printf("\n\nEnter the position of the element:");
scanf("%d",&pos);
n=delpos(pos,n,a);
printf("\n The array after deletion is:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
else if(choice==2)
{
printf("\n Enter the element you want to
delete:");
scanf("%d",&ele);
for(i=0;i<n;i++)
{
if(a[i]==ele)
{
flag=1;
n=delpos(i,n,a);
break;
}
}
if(flag==0)
printf("\n Element not found");
printf("\n The array after deletion is:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
else
printf("\n Invalid choice");
printf("\n\n Do you wish to continue (y/n):");
fflush(stdin);
ch=getchar();
}while(ch=='y');
printf("\n\n\n END OF PROGRAM");
return 0;
}

int delpos(int p,int n,int a[])


{
int i,j;
if(p>=n)
{
printf("\n Invalid position \n Again
enter the position:");
scanf("%d",&p);
}
if(p==(n-1))
{
n--;
return n;

}
else
{
for(j=p;j<n;j++)
a[j]=a[j+1];
n--;
return n;

}
}

Das könnte Ihnen auch gefallen