Showing posts with label array insertion operation in c. Show all posts
Showing posts with label array insertion operation in c. Show all posts

Thursday, March 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