add new item in array in C
Code snippet on how to add new item in array in C
int array[10], new_item, position;
//input the new item and position
scanf("%d%d",&new_item,&position);
//update the elements after the specified position
int i;
for(i=9; i>=position-1; i--)
{
array[i+1] = array[i];
}
//insert the new item at specified position
array[position-1] = new_item;
This code snippet adds a new item to an array of size 10. It requires two inputs from the user - the value of the new item and the position at which it should be added. The loop which begins from the last element (index 9) of the array, moves each element to the next index by assigning the value of the preceding element to its own index. Once the loop is complete, the new item is assigned to the position specified by the user. This process adds the new item at the specified position and shifts the other elements by one index.