Home:ALL Converter>Inserting element in an array with position

Inserting element in an array with position

Ask Time:2019-05-15T01:04:57         Author:Imbecile

Json Formatter

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.

Author:Imbecile,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/56135315/inserting-element-in-an-array-with-position
yy