Saturday, August 20, 2011

Insert an element in array.

//Program to insert an element in array.
#include<stdio.h>
#include<conio.h>
void main()
{
 int i,data[30],n,m,a,l,e;
 char c;
 clrscr();
 printf("program to insert element in array");
 printf("\nEnter the number of elements in the array:");
 scanf("%d",&n);
      a=0;
      l=n-1;
 for(i=a;i<=l;i++)
 {
  printf("\nEnter the value of %d element:",i);
  scanf("%d",&data[i]);
 }
 printf("\nArray before insertion: ");
 for(i=0;i<n;i++)
 printf("%d, ",data[i]);
  printf("\nEnter the element you want to insert: ");
  scanf("%d",&e);
 printf("\nif u want to insert beginning element then press b");
 printf("\nif u want to insert middle element then press m");
 printf("\nif u want to insert end element then press e");
 printf("\nEnter ur choice: ") ;
 c=getche();
 switch(c)
 {
  case 'b':
  case 'B':

      for(i=l;i>=0;i--)
      {
       data[i+1]=data[i];
      }
      data[a]=e;
      l=l+1;
      break;
  case 'm':
  case 'M':
      m=(a+l)/2;
      for(i=l;i>=m;i--)
      {
    data[i+1]=data[i];
      }
      data[m]=e;
      l=l+1;
     break;
  case 'e':
  case 'E':
     l=l+1;
     data[l]=e;
     break;
  default:
       printf("\nInvalid choice");
       getch();
       exit(0);
 }
  printf("\nArray after insertion:");
  for(i=a;i<=l;i++)
  printf("%d, ",data[i]);
 getch();
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More