3.22.2012

Insert element in array

Q. Write a C program to insert or add new element in array, give with output of program.


Ans.


/*c program for insert new element in array*/
#include<stdio.h>
#include<conio.h>
#define SIZE 50
int main()
{
 int arr[SIZE];
 int i,index,tmp,num;
 printf("Enter total number of elements in array : ");
 scanf("%d", &num);
 for(i=0; i<num; i++)
 {
   printf("Enter %d element : ",i+1);
   scanf("%d", &arr[i]);
 }
 printf("\nEnter element number before to insert new element : ");
 scanf("%d", &index);
 printf("Enter new element : ");
 scanf("%d", &tmp);
 for(i=num-1; i>=index-1; i--)
    arr[i+1] = arr[i];
 arr[index-1] = tmp;
 printf("\n-- After insertion an element, new array list --\n\n");
 for(i=0; i<=num; i++)
   printf("\t%d\n",arr[i]);
 getch();
 return 0;
}


/**************** Output ******************/
Screen shot of insert element in array




Related programs:

  1. How to create array in C?
  2. How to calculate length of array?
  3. Display array in reverse order
  4. Delete an exist element in array
  5. Example of array C program

1 comment:

  1. #include
    main()
    {
    int a[5],i=0,j=0,p=0,e=0,k=0;
    e=sizeof(a)/sizeof(a[0]);
    printf("%d\n",e);
    // k=e;
    // printf("%d",k+1);
    printf("Enter the array") ;
    for(i=0;i=p;i--)
    {

    a[i+1]=a[i];



    }


    a[p]=169;

    printf("new array\n");
    for(i=0;i<=e;i++)
    printf("%d\n",a[i]);
    }












    ReplyDelete