Write a C Program to insert an element in an Array.
An array is a collection of similar data elements stored in a contiguous memory location.
Example: arr[5] = {2,7,1,23,5}
The new element we are entering here is 15 and the position where the element must be inserted is “4”.
1. Declare a one-dimensional array of some fixed capacity.
2. Take size of the array as input from users
3. Define array elements, taking each element as input from users.
4. Get the number to be inserted.
5. Get the position where element needs to be inserted.
6. From that position shift all elements to one position forward.
7. Insert the element in that position.
8. Exit.
Here is the source code of the C program to insert an element into an Array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to insert an element in a specified position in a given array
*/
#include <stdio.h>
void main()
{
int array[100];
int i, n, x, pos;
printf("Enter the number of elements in the array \n");
scanf("%d", &n);
printf("Enter the elements \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements are: \n");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\nEnter the new element to be inserted: ");
scanf("%d", &x);
printf("Enter the position where element is to be inserted: ");
scanf("%d", &pos);
//shift all elements 1 position forward from the place
//where element needs to be inserted
n=n+1;
for(i = n-1; i >= pos; i--)
array[i]=array[i-1];
array[pos-1]=x; //Insert the element x on the specified position
//print the new array
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
}
1. Initialize an array of size 100.
2. Ask the user for number of elements they want to enter into the array. Store it in variable n.
3. Take n number of elements as input from the user and store it in the array.
4. Ask the user for new element, x to be inserted and the position pos where element needs to be inserted.
5. Increment the value of n by 1.
5. Shift all the elements that are next to pos to one index forward.
6. Now, array[pos] is empty, insert the element x into the array.
7. Print all the elements.
Example:
- Consider an example with n=5 and array elements as 2, 7, 1, 23, 5.
- Now ask the user for x and pos. Suppose the value entered by user are x=15 and pos=4.
- Increment the value of n by 1 i.e., n=6. Run a for loop from i=n-1 = 5 to i=pos = 4 and in each iteration insert the element at previous index i.e., i-1 to index i. Therefore, array[5] = array[4] which is equal to 5.
- Decrement the value of i by 1 and repeat the same process array[4] = array[3] i.e., array[4] = 23.
- Now i=3 and pos=4 so condition in for loop becomes false, come out of the loop.
- Now, insert the element x in index pos-1 i.e., array[3] = 15.
- The element is inserted now, run a for loop and print the array with element being inserted. i.e. 2 7 1 15 23 5
Time Complexity: O(n)
The above program for inserting an element in an array has a time complexity of O(n), as the for loop runs from 0 to n.
Space Complexity: O(n)
In the above program, space complexity is O(n) as an array of size n has been initialized to store the values in it.
In this case, the new element is 15 and the position where the element to be inserted is “4”.
Enter the number of elements in the array 5 Enter the elements 2 7 1 23 5 Input array elements are: 2 7 1 23 5 Enter the new element to be inserted: 15 Enter the position where element is to be inserted: 4 2 7 1 15 23 5
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Apply for C Internship
- Practice BCA MCQs
- Buy Computer Science Books