Implementation
The following is how the above approach is implemented:
In the program below, we take an array as user input and then ask the user for a new number to add to the original array and the position where the new number should be added.
Then we move the current numbers one place to the right from the position where the new number has to be added to the end of the array, freeing up room for the new element. The new number is subsequently added to the user-specified position index.
#include<stdio.h>
int main()
{
int array[100],loc,size, value;
// Enter number of elements in array
scanf("%d", &size);
// Enter the elements
for(int i = 0; i < size; i++)
scanf("%d", &array[i]);
// Enter the position you want to insert in the array
scanf("%d", &loc);
// Enter the value you want to insert at that position in the array
scanf("%d", &value);
// shifting the elements from that location to size of array to right
for(int i = size-1; i >= loc-1; i--)
array[i+1] = array[i];
array[loc-1] = value; // inserting the given value
printf("Resultant array is: ");
/*
the array size gets increased by 1
after insertion of the element
*/
for(int i = 0; i <= size; i++)
printf("%d ", array[i]);
return 0;
}
![](https://files.codingninjas.in/fluent_code-24-filled-29586.svg)
You can also try this code with Online C Compiler
Run CodeSample Input 1
7
7 6 5 4 3 2 1
4
30
Sample Output 1
Resultant array is: 7 6 5 30 4 3 2 1
Sample Input 2
9
2 4 6 8 10 12 12 16 18
7
14
Sample Output 2
Resultant array is: 2 4 6 8 10 12 14 12 16 18
You can practice by yourself with the help of online c compiler.
We are done with the blog, lets’ move to FAQS.
Frequently Asked Questions
What is insert in an array?
Inserting in an array involves adding a new element at a specific position within the array, shifting existing elements if necessary to make space. This operation helps in managing data dynamically, though it can require shifting multiple elements in fixed-size arrays.
Why do we need to insert an element in an array?
Inserting an element in an array is essential for dynamically managing data, such as adding new information without overwriting existing values. This operation enables more flexible data manipulation within a fixed structure.
What are the common challenges when inserting an element in an array?
Common challenges in inserting an element include shifting elements to make space, which can be time-consuming in large arrays, and managing fixed array sizes that limit the capacity to add new elements without resizing.
What is the average time complexity of insertion in an array?
The average time complexity of insertion in an array is O(n).
Conclusion
This article provided an in-depth look at implementing a C program to insert an element in an array, covering the algorithm and its time complexity. For further insights into arrays and their operations, be sure to explore additional resources to enhance your understanding.
Check out the following problems -
We hope that this blog has helped you enhance your knowledge regarding insertion in an Array and if you would like to learn more, check out our articles in the code studio library. Do upvote our blog to help other ninjas grow. Happy Coding!