Q:

C Program To Insert An Element Desired or Specific Position In An Array

belongs to collection: Array C Programs List

0

An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in C.
Given an array arr of size n, this article tells how to insert an element x in this array arr at a specific position pos.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<stdio.h>
int main()
{
  int i,arr[50],pos,len;
  int newitem;

  printf("Enter no. of elements you want for the array:\n");
  scanf("%d",&len);

  printf("Enter %d elements for the array:\n",len);
  for(i=0;i<len;i++)
            scanf("%d",&arr[i]);

  printf("Enter the element you want to insert:\n");
  scanf("%d",&newitem);
  printf("Enter position for the newitem element:\n");
  scanf("%d",&pos);

  len ++;
  pos --;
  i = len-1;

  while(i>=pos){
            arr[i]=arr[i-1];
            i--;
  }
  arr[pos] = newitem;
  printf("Array after inserting newitem element\n");

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

  return 0;
}

 

Output:

Enter no. of elements you want for the array:

5

Enter 5 elements for the array:

23

65

36

14

52

Enter the element you want to insert:

65

Enter position for the newitem element:

3

Array after inserting newitem element

 23 65 65 36 14 52

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program For Remove Duplicates Items In An Array... >>