2/23/2009

C Program To Perform Insertion, Deletion And Display Operations On Array


/* C Program To Perform Insertion, Deletion And Display Operations On Array */
#include<stdio.h>
#include<conio.h>
void main()
{
  int i,n,a[10],loc,ele,choice,temp;
  clrscr();
 printf("Enter the size of array\n");
  scanf("%d",&n);
 printf("Enter the elements\n");
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 printf("Entered array is\n");
 for(i=0;i<n;i++)
  printf("%d\t",a[i]);

 for(;;)
  {
  printf("\n1:Insertion  2:Deletion 3:Dispaly 4:Exit\n");
  printf("Enter your choice\n");
   scanf("%d",&choice);

  switch(choice)
   {
    case 1:
    printf("Enter the location and the elements\n");
     scanf("%d%d",&loc,&ele);
    loc=loc-1;

    for(i=0;i<n;i++)
     {
        if(i==loc)
      a[i]=ele;
     }
    printf("%d is inserted at %d\n",ele,loc+1);
    break;
    case 2:
    printf("Enter the location of element to be deleted\n");
     scanf("%d",&loc);
    if(n<loc)
     {
       printf("There is no element in that location\n");
       break;
     }
    loc=loc-1;
    temp=a[loc];

    if(loc==n)
     {
       n--;
       printf("Deleted element is %d\n",temp);
       break;
     }
    else if(loc<n)
     {
     for(i=loc;i<n;i++)
      {
        a[i]=a[i+1];
      }
     }
    n--;
    printf("Deleted element is %d\n",temp);
    break;
    case 3:
    if(n<=0)
     printf("Error: the array is empty\n");
     printf("Array is \n");
     for(i=0;i<n;i++)
      printf("%d\t",a[i]);
     printf("\n");
     break;
    case 4:
    exit(0);
    break;
    default:
    printf("Invalid choice\n");
    break;
   }
  getch();
  }
}
Out Put:
C Program To Perform Insertion, Deletion And Display Operations On Array

No comments:

Post a Comment