C Program To Insert An Element in An Array - Sanfoundry
C Program To Insert An Element in An Array - Sanfoundry
Ads by
Stop seeing this ad Why this ad?
Problem Description
Write a C Program to insert an element in an Array.
What is Array?
An array is a collection of similar data elements stored in a contiguous memory location.
The new element we are entering here is 15 and the position where the element must be inserted is
“4”.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
https://www.sanfoundry.com/c-program-insert-element-array/ 1/8
9/12/23, 9:33 PM C program to Insert an Element in an Array - Sanfoundry
Problem Solution
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.
Program/Source Code
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.
1. /*
2. * C program to insert an element in a specified position in a given array
3. */
4.
5. #include <stdio.h>
6. void main()
7. {
8. int array[100];
9. int i, n, x, pos;
10.
11. printf("Enter the number of elements in the array \n");
12. scanf("%d", &n);
13. printf("Enter the elements \n");
14.
15. for (i = 0; i < n; i++)
16. {
17. scanf("%d", &array[i]);
18. }
https://www.sanfoundry.com/c-program-insert-element-array/ 2/8
9/12/23, 9:33 PM C program to Insert an Element in an Array - Sanfoundry
19.
20. printf("Input array elements are: \n");
21. for (i = 0; i < n; i++)
22. {
23. printf("%d ", array[i]);
24. }
25. printf("\nEnter the new element to be inserted: ");
26. scanf("%d", &x);
27. printf("Enter the position where element is to be inserted: ");
28. scanf("%d", &pos);
29.
30. //shift all elements 1 position forward from the place
31. //where element needs to be inserted
32. n=n+1;
33. for(i = n-1; i >= pos; i--)
34. array[i]=array[i-1];
35.
36. array[pos-1]=x; //Insert the element x on the specified position
37.
38. //print the new array
39. for (i = 0; i < n; i++)
40. {
41. printf("%d ", array[i]);
42. }
43. }
Program Explanation
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:
https://www.sanfoundry.com/c-program-insert-element-array/ 3/8
9/12/23, 9:33 PM C program to Insert an Element in an Array - Sanfoundry
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
advertisement
https://www.sanfoundry.com/c-program-insert-element-array/ 4/8
9/12/23, 9:33 PM C program to Insert an Element in an Array - Sanfoundry
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data
Structures in C” and “Algorithms in C”.
advertisement
Next Steps:
Related Posts:
advertisement
Recommended Articles:
1. C program to Insert an Element in the Sorted Array
2. Java Program to Insert an Element in an Array
3. C Program to Find Largest Element in an Array
4. C program to Delete an Element from an Array
5. Java Program to Print Next Greater Element in Array
6. C++ Program to Find Largest and Smallest Element of an Array
7. C Program to Find the kth Element in an Array
8. C Program to Find Largest Element in an Array using Recursion
9. C Program to Search an Element in an Array
10. C++ Program to Implement Array in STL
advertisement
https://www.sanfoundry.com/c-program-insert-element-array/ 6/8
9/12/23, 9:33 PM C program to Insert an Element in an Array - Sanfoundry
Additional Resources:
Java Array Programs
C# Array Programs
C Programs on Arrays
Searching and Sorting Algorithms in C
Finite Element Method MCQ Questions
Popular Pages:
Sorting Algorithms in Java
Searching Algorithms in Java
C++ Algorithm Library
Data Structure MCQ Questions
Visual Basic MCQ Questions
Name
Subscribe
https://www.sanfoundry.com/c-program-insert-element-array/ 7/8
9/12/23, 9:33 PM C program to Insert an Element in an Array - Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is
Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on
development of Linux Kernel, SAN Technologies, Advanced C, Data
Structures & Alogrithms. Stay connected with him at LinkedIn.
https://www.sanfoundry.com/c-program-insert-element-array/ 8/8