Tuesday, 29 July 2014

A. WRITE A MENU DRIVEN C PROGRAM TO A. INSERT AN ELEMENT INTO AN ARRAY B. DELETE AN ELEMENT FROM THE ARRAY (FIRST OCCURRENCE)



 A. WRITE A MENU DRIVEN C PROGRAM TO
A. INSERT AN ELEMENT INTO AN ARRAY
B. DELETE AN ELEMENT FROM THE ARRAY (FIRST OCCURRENCE)
SOURCE CODE:
int a[100];
void main()
{
int n,r,i;
printf("Please enter the size of the array");
scanf("%d",&n);
printf("\nPlease enter Elements of the array\n");
for (i= 0; i< n;i++)
scanf("%d",&a[i]);
printf("Please enter the number to be removed: ");
scanf("%d",&r);
i= remove(n,r);
if (i!= -1)
{
printf("%d is found at %d position and removed\n",r,i);
printf("Contents of the array After deletion\n");
for (i= 0; i< n-1;i++)
printf("%d\n",a[i]);
}
else
printf("Item Not Found!!!");
}
int remove(int n, int r)
{
int i,c,j;
for (i= 0; i < n; i++)
if (a[i] == r){
j=i;
for ( c = i ; c < n - 1 ; c++ )
a[c] = a[c+1];
return i;
}
return -1;
}

0 comments:

Post a Comment

 
- |