INSERT AN ELMENT IN AN ARRAY AT DESIRED POSITION USING C PROGRAM


How to insert or add an element in the array at specific or desired posting by using c programming language? Source code is as follow:


#include<stdio.h>
int main(){
int a[50],size,num,i,pos,temp;
printf("\nEnter size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ",size);
for(i=0;iscanf("%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{
a[temp]=a[temp-1];
temp--;
}
a[i]=num;
for(i=0;iprintf(" %d",a[i]);
return 0;
}

11 comments:

  1. insertion of an element using for loop..

    printf("\nEnter the size of the array:");
    scanf("%d",&n);
    printf("Enter the elements:");
    for(i=0;i=pos-1;i--)
    {
    a[i+1]=a[i];
    }
    a[pos-1]=num;
    n++;
    printf("\nElements in the array...\n");
    for(i=0;i<n;i++)
    printf("%d\n",a[i]);

    ReplyDelete
  2. i cant understand please explain this

    ReplyDelete
  3. but program required for inserting element into an existing array

    ReplyDelete
  4. Make this program using structure.Please.I need it.

    ReplyDelete
  5. i am not understand these example.
    you make more complicated.
    plz publish simple eg so that will help the student.
    and add your description also ....

    ReplyDelete
  6. can not understand this program

    ReplyDelete
  7. how about a element insertion with a function of 4 arguments?
    one for the array, the other for lenth, index and the desired value

    ReplyDelete
  8. *insertion at any pos in an array by hitian*

    #include
    int main(){
    int a[50],i,pos,size,item;
    printf("\nEnter size of the array: ");
    scanf("%d",&size);

    printf("\nEnter %d elements in to the array: ",size);
    for(i=0;i=pos-1)
    {
    a[i+1]=a[i];
    i--;

    }

    a[pos-1]=item;

    for(i=0;i<=size;i++)
    printf(" %d",a[i]);

    return 0;
    }

    ReplyDelete
    Replies
    1. //error hai thoda thik karle..

      #include
      int main(){
      int a[50],i,pos,size,item;
      printf("\nEnter size of the array: ");
      scanf("%d",&size);

      printf("\nEnter %d elements in to the array: ",size);
      for(i=0;i=pos-1)
      {
      a[i+1]=a[i];
      i--;

      }

      a[pos-1]=item;

      for(i=0;i<=size;i++)
      printf(" %d",a[i]);

      return 0;
      }

      Delete
  9. please describe the program, how does it work?

    ReplyDelete
  10. #include

    int main()
    {
    int array[100], position, c, n, value;

    printf("Enter number of elements in array\n");
    scanf("%d", &n);

    printf("Enter %d elements\n", n);

    for (c = 0; c < n; c++)
    scanf("%d", &array[c]);

    printf("Enter the location where you wish to insert an element\n");
    scanf("%d", &position);

    printf("Enter the value to insert\n");
    scanf("%d", &value);

    for (c = n - 1; c >= position - 1; c--)
    array[c+1] = array[c];

    array[position-1] = value;

    printf("Resultant array is\n");

    for (c = 0; c <= n; c++)
    printf("%d\n", array[c]);

    return 0;
    }

    ReplyDelete