# C PROGRAMS
Array Programs
➤ Find Length of Array
➤ How to Print an Array
➤ Sum of Array Elements
➤ Reverse an Array in C
➤ Copy an Array in C
➤ Merge Two Arrays in C
➤ Merge Two Sorted Arrays
➤ Count Repeated Elements
➤ Find Duplicate Elements
➤ Linear Search in C
➤ Binary Search in C
➤ Insert Element in Array
➤ Delete Element in Array
➤ Display odd-even in Array
➤ Sum & Count of Odd-Even
➤ Count +ve, -ve, & 0
➤ Sum of +ve, -ve Numbers
➤ Avg & Numbers > Avg
➤ Smallest & Largest Element
➤ 1st 2nd Max-Min
➤ Sort Array in C
➤ Search element in Array
➤ Search index of Nth times occurred element
➤ Matrix Programs
C program to insert an element in an array | Here we will write a C program to insert a new element entered from the user at a particular position in a given list of numbers.
Example1:-
Array = {10, 25, 15, 14, 30}
Position = 2
Element = 6
Resultant array = {10, 25, 6, 15, 14, 30}
Note:- Array index starts from 0, not from the 1.
Example2:-
Array = {10, 25, 15, 14, 30}
Position = 8
Element = 6
Error! Position is not valid.
Enter position from 0 to 5.
The position from 0 to the length of array are valid. If the position is 0 then element will be inserted at the starting of the array and if the position = length of original array then element will be inserted at the end of the array.
We can’t insert an element when the position is lesser than 0 because the array index always starts with 0. When the new element position is greater than the last position+1 of the array then again we can’t insert the element.
Approach to insert an element in an array,
- Take an array, position, and element.
- Check position is valid or not. If the position is invalid then don’t go to the next step.
- Create a new array with the size of n+1, where n is the size of the original array.
- Copy the element of the original array to the new array from 0 to position-1 index.
- Insert an element at position index.
- Copy remaining elements of the original array to the new array.
C Program to Insert an Element in the given Array
#include<stdio.h>
int main()
{
// original array
int arr[] = {10, 20, 30, 40, 50};
// take position and element
int index, key;
printf("Enter position: ");
scanf("%d", &index);
printf("Enter element to insert: ");
scanf("%d", &key);
// calculate size of the array
int n = sizeof(arr)/sizeof(arr[0]);
// check position
if(index < 0 || index > n)
{
printf("Error! The position is not valid.");
printf("\nPlease, Enter position from 0 to %d\n", n);
return 0;
}
// create new array of size = n+1
int temp[n+1];
// copy elements
for (int i=0, j=0; i <= n; ++i)
{
if(i == index) temp[i] = key;
else temp[i] = arr[j++];
}
// display new array
printf("Array elements are: \n");
for (int i = 0; i <= n; ++i)
{
printf("%d ", temp[i]);
}
return 0;
}
Output:-
Enter position: 0
Enter element to insert: 55
Array elements are:
55 10 20 30 40 50
Enter position: 5
Enter element to insert: 99
Array elements are:
10 20 30 40 50 99
Enter position: 9
Enter element to insert: 100
Error! The position is not valid.
Please, Enter position from 0 to 5
Enter position: -2
Enter element to insert: 10
Error! The position is not valid.
Please, Enter position from 0 to 5
Also see:- C program to find the sum of elements in an array
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!