1var colors=["red","blue"];
2var index=1;
3
4//insert "white" at index 1
5colors.splice(index, 0, "white"); //colors = ["red", "white", "blue"]
6
1/*
2 * Program : Inserting an element in the array
3 * Language : C
4 */
5
6#include<stdio.h>
7
8#define size 5
9
10int main()
11{
12 int arr[size] = {1, 20, 5, 78, 30};
13 int element, pos, i;
14
15 printf("Enter position and element\n");
16 scanf("%d%d",&pos,&element);
17
18 if(pos <= size && pos >= 0)
19 {
20 //shift all the elements from the last index to pos by 1 position to right
21 for(i = size; i > pos; i--)
22 arr[i] = arr[i-1];
23
24 //insert element at the given position
25 arr[pos] = element;
26
27 /*
28 * print the new array
29 * the new array size will be size+1(actual size+new element)
30 * so, use i <= size in for loop
31 */
32 for(i = 0; i <= size; i++)
33 printf("%d ", arr[i]);
34 }
35 else
36 printf("Invalid Position\n");
37
38 return 0;
39 }
40