Skip to content

Instantly share code, notes, and snippets.

@jamesgeorge007
Last active February 22, 2018 12:55
Show Gist options
  • Save jamesgeorge007/ab2dc82e768fc2815ce5e872e19f21f8 to your computer and use it in GitHub Desktop.
Save jamesgeorge007/ab2dc82e768fc2815ce5e872e19f21f8 to your computer and use it in GitHub Desktop.
Implementation of the basic Array manipulation operations like searching, deleting and inserting elements as long as the user wants in the C language.
#include<stdio.h>
void main()
{
int n, A[25], i, el, pos, choice, position, index = 0;
char cont='y';
printf("Number of elements:");
scanf("%d", &n);
printf("\n Enter the elements:");
for(i=0;i<n;i++)
scanf("%d", &A[i]);
do{
printf("\n\n1.Delete an element\n\n2.Insert an element\n\n3.Search for an element\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nPosition of the element to be deleted: ");
scanf("%d", &pos);
if(pos<=n)
{
for(i=pos-1; i<n-1;i++)
{
A[i]=A[i+1];
}
printf("\n\nAfter deleting the element at position %d, the array is:\n", pos);
for(i=0;i<n-1;i++)
printf("%d ", A[i]);
n--;
if(n==0)
{
printf("\nNo more elements present to be deleted. You may add more elements.");
break;
}
}
else
{
if(n==0)
printf("\nThe array is empty!");
else
printf("\nOnly positions up to %d is allowed!.", n);
}
break;
case 2:
printf("\n\nEnter the element to be inserted:");
scanf("%d", &el);
printf("\n\nPosition:");
scanf("%d", &position);
if(position<=n)
{
for(i=n-1;i>=position;i--)
{
A[i]=A[i-1];
}
A[position-1] = el;
printf("\nThe array after inserting %d at the position %d is: \n", el, position);
for(i=0;i<n+1;i++)
printf("%d ", A[i]);
n++;
}
else
{
printf("\nPosition should be <= %d", n);
}
break;
case 3:
printf("Element to be searched: ");
scanf("%d", &el);
for(i=0;i<n;i++)
{
if(A[i] == el)
{
index = i;
break;
}
}
if(index == 0)
{
printf("\n%d is not found within the array.", el);
}
else
{
printf("\n%d found at location %d", el, index+1);
}
}
printf("\nWant to continue? (Y/y)");
cont = getch();
}while(cont == 'y' || cont == 'Y');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment