Wednesday, April 18, 2012

INSERT AN ELMENT IN AN ARRAY AT DESIRED POSITION

#include<stdio.h> 
#include<conio.h>
 
void main()
{
  int a[50],size,num,i,pos,temp;
  clrscr();
  printf("\nEnter size of the array: ");
  scanf("%d",&size);
  printf("\nEnter %d elements in to the array: ",size);
  for(i=0;i<size;i++)
            scanf("%d",&a[i]);
  printf("\nEnter position and number to insert: ");
  scanf("%d %d",&pos,&num);
  i=0;
  while(i!=pos-1)
            i++;
  temp=size++;
  while(i<temp)
  {
            a[temp]=a[temp-1];
            temp--;
  }
  a[i]=num;
  for(i=0;i<size;i++)
            printf(" %d",a[i]);
  getch();
}

No comments:

Post a Comment