insert delete search sort the element

Create a Array to insert, delete, search & sort the element on that list.

#include<stdio.h>
#include<conio.h>
int insert (int[],int);
int del (int[],int);
void display (int[],int);
void sort (int[],int);
void search (int[],int,int);
int main ()
{
	int i,ch=0,a[10],n,s;
   clrscr();
   printf("How many no you want to enter:");
   scanf("%d",&n);
   printf("Enter the elements: ");
   for(i=0;i<n;i++)
   	scanf("%d",&a[i]);
   while(ch!=6)
   {
   	printf("\n1. Insertion\n2. Deletion\n3. Display\n4. Sorting\n5. Search\n6. Exit\n");
   	scanf("%d",&ch);
      switch(ch)
      {
      	case 1:
         	n=insert(a,n);
         	break;
         case 2:
         	n=del(a,n);
         	break;
         case 3:
         	display(a,n);
         	break;
         case 4:
         	sort(a,n);
         	break;
         case 5:
         	printf("Enter a no. for searching: ");
            scanf("%d",&s);
            search(a,n,s);
            break;
         default:
         	printf("You enter wrong key!\n");
         	break;
      }
   }
   return 0;
}

void display(int a[],int n)
{
	int i;
   printf("The elements are: ");
   for(i=0;i<n;i++)
   	printf("%d ",a[i]);
}

int insert(int a[],int n)
{
   int p,e,i;
	printf("Enter the position: ");
   scanf("%d",&p);
   printf("Enter the element: ");
   scanf("%d",&e);
   for(i=n-1;i>=p-1;i--)
   	a[i+1]=a[i];
   a[i+1]=e;
   n++;
   return n;
}

int del(int a[],int n)
{
   int p,i;
	printf("Enter the position: ");
   scanf("%d",&p);
   for(i=p;i<n;i++)
		a[i-1]=a[i];
   n--;
   return n;
}

void sort (int a[],int n)
{
	int cs,i,j;
   printf("1. Asending\n2. Desending");
   scanf("%d",&cs);
   switch(cs)
   {
   	case 1:
      	for(i=0;i<n-1;i++)
			{
				for(j=i+1;j<=n-1;j++)
            {
            	if(a[i]>a[j])
					{
						a[i]=((a[i]+a[j])-(a[j]=a[i]));
					}
				}
			}
         break;
      case 2:
      	for(i=0;i<n-1;i++)
			{
				for(j=i+1;j<=n-1;j++)
				{
					if(a[i]<a[j])
					{
						a[i]=((a[i]+a[j])-(a[j]=a[i]));
					}
				}
			}

         break;
         default:
         	printf("\nWRONG!\n");
   }
}

void search (int a[],int n,int s)
{
	int i;
	for(i=0;i<n;i++)
   {
   	if(s==a[i])
      {
      	printf("\nElement found! position is: %d\n",i+1);
      	break;
      }
   }
   if(s!=a[i])
   	printf("\nNO data found!\n");
}

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *