C Program For Inserting an Element In an Array
Introduction
In this blog, we will learn about the insertion of elements in an array, so let's begin with an introduction of the arrays and then we will learn how we operate on them. A collection of data components of the same kind stored in contiguous memory areas is known as an array. The array is the most basic data structure, in which each data member may be retrieved at random by its index number.
We may retrieve the items by utilizing the array. To access the array's elements, only a few lines of code are necessary. We'll look at how to insert an element into an array in C in this blog.
Also Read, Binary to Hex Converter.
Algorithm
For the Insertion of elements in an array, we have multiple scenarios like we can insert an element at the beginning of the array, as well as the end of the array of even middle of the array or anywhere else, so we should consider all cases while writing out algorithm.
The locations or index location of the element in the array is increased once it is inserted, but this does not imply that the array's size is rising.
The logic for inserting the element is as follows:
- To start, obtain the element to be added, such as x.
- Then, obtain the location where this element will be put, say pos.
- Then shift the array items one position ahead, then do the same for all the other elements right to pos. Because the location pos is now empty, insert the element x there.
The Time Complexity is O(n) as we have to traverse the whole array.
You can also read about dynamic array in c.
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;
}
Sample 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.
FAQs
- Where arrays should be used?
Arrays should be used where the number of elements to be stored is already known and Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched. - What is the average time complexity of insertion in an array?
The average time complexity of insertion in an array is O(n). - Why is array insertion O(n)?
Inserting an element in the center of an array has a computational cost of O(n), as, After the insertion, all of the items in the array must be pushed up one index, or all of the elements must be copied to a new array large enough to contain the added element. - Which is better, O(n) or O(log n)?
The maximum running time of the method is proportional to the amount of input. O(something) is an upper bound on the number of instructions in an algorithm (atomic ones). As a result, O(logn) is more compact than O(n), and it also performs better in algorithm analysis. - Can pointers be helpful without an array?
Although an array name may be used as a pointer and array notation can be used with pointers, the two are separate and cannot always be used interchangeably.
Key Takeaways
In this article, we have extensively discussed C Program to insert an element in an Array and have learned about the algorithm's implementation and its time complexity. To learn more about Arrays, you can check out here.
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!