0% found this document useful (0 votes)
15 views

C Program To Insert An Element in An Array - Sanfoundry

1. This document describes a C program to insert an element into an array at a specified position. 2. It explains that the program takes user input for the array size and elements, the element to insert, and the insertion position. 3. The program shifts existing elements after the insertion position forward by one, makes space for the new element, and inserts it at the specified position.

Uploaded by

aryanraj22760
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

C Program To Insert An Element in An Array - Sanfoundry

1. This document describes a C program to insert an element into an array at a specified position. 2. It explains that the program takes user input for the array size and elements, the element to insert, and the insertion position. 3. The program shifts existing elements after the insertion position forward by one, makes space for the new element, and inserts it at the specified position.

Uploaded by

aryanraj22760
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

9/12/23, 9:33 PM C program to Insert an Element in an Array - Sanfoundry

Ads by
Stop seeing this ad Why this ad?

C program to Insert an Element in an Array


« Prev Next »

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.

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”.

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.

Check this: C Books | Computer Science Books

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.

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

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.

advertisement

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.

Runtime Test Cases


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

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

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”.

advertisement

« Prev - C Program to Reverse an Array » Next - C program to Convert Fahrenheit to


Celsius

Next Steps:

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

Related Posts:

Apply for C Internship


Apply for Computer Science Internship
Buy C Books
Buy Computer Science Books
https://www.sanfoundry.com/c-program-insert-element-array/ 5/8
9/12/23, 9:33 PM C program to Insert an Element in an Array - Sanfoundry

Practice Computer Science MCQs

C Programs, C Programs - Array

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

Subscribe: C Programming Newsletter

Name

Email

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

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to


get free Certificate of Merit. Join our social networks below and stay updated with latest contests,
videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest

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.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram


SanfoundryClasses.

About | Certifications | Internships | Jobs | Privacy Policy | Terms | Copyright | Contact

     

© 2011-2023 Sanfoundry. All Rights Reserved.

https://www.sanfoundry.com/c-program-insert-element-array/ 8/8

You might also like