When inserting into an array, we know the index starts from 0. So, if we want to insert an element at position 3, we've to enter the input at position 2. For readability I wanted to give the proper location, i.e., position 3 means at exact 3, not 2.
Here is the code snippet.
printf("In which position you want to enter the element? ");
scanf("%d",&k);
for (j=n; j>=k; j--)
{
array[j+1]=array[j];
}
printf("Which element do you want to insert? ");
scanf("%d", &item);
array[k]=item;
n++;
Sample output:
How many elements? 5
Enter the values
1
2
4
5
6
In which position you want to enter the element? 2
Which element do you want to insert? 3
After insertion the array is:
1
2
3
4
5
6
I want the position to be at 3.