Insert Element in Array in C Language
Program Description:
The program should take a user-supplied input array, the position in which to insert a new element, and the element itself. Then it should insert the specified number(element) in that position(location).
📢This program is part of the Array Practice Programs in C language.
Here is the example input and output of the array insertion program.
Excepted Input and Output:
Input:
Enter desired array size(1-1000): 5
Please enter array elements(5) : 1 2 3 4 5
Enter the position(1-5) : 5
Enter the value to insert: 1000
Output:
Original Array: 1 2 3 4 5
Array after inserting element: 1 2 3 4 1000 5
Inserting an element in an array
Prerequisites:
We are going to use the C Arrays and Functions. So it is good idea to know the basics of the Arrays and functions, Please go through the following articles to learn more
- What are Arrays in Programming – How to Create, Use Arrays in C Programming
- Functions in C – How to Declare, Define, and Call Functions
- How to Pass Arrays to Functions in C
Insert Element in Array in C Explanation:
- Declare an array named numbers[1000] to store the input array. The numbers array can hold 1000 elements. Change the size if you want to try with larger size arrays.
- Take the desired array size from the user and store it in size array.
- Create a function called
read(). The
read() function uses a For loop to iterate over the input array and update the array elements with the user-provided values.
- The read() function takes two formal arguments numbers[] array and it’s size
- Call the read() function from the main() function to update the array elements.
- Prompt the user to provide the
position where a new element needs to be inserted into the array. Store the value in
position variable.
- Note, We are considering that position starts from 1. So position = index + 1.
- Also, take the number to be inserted from the user and store it in the variable iNum.
- Define another function called display() to print the array elements on the console. call the display() function from the main() function and pass the numbers[] array and it’s size as parameters.
- To Insert element at
position , We need to move the elements from
position till the end of the array by one position right. Now we can insert the element at the
position.
- Start a For Loop from the index size till the position index in reverse order – for(i = size; i>= position; i–)
- Move the element by one position right side. So copy the numbers[i-1] to number[i].
- Repeat the above loop til the i reaches the position.
- Once the above loop is completed, Insert the given number ( iNum) at the position-1 index. – numbers[position-1] = iNum;
- Increment the array size by 1. (As we inserted a new element to the array)
- Display the Array after insertion on the console by calling the display() function.
Algorithm to Insert Element in Array in C:
- Declare numbers[1000] array and i, size, iNum, and position variables
- Take the array size from the user – scanf("%d", &size);
- Call read() function to update the array with user input – read(numbers, size);
- Take the position and iNum from the user
- Display the original array by calling display() function – display(numbers, size);
- For Loop : start from
<strong> i=size</strong> to i>=position
- numbers[i] = numbers[i-1];
- i--;
- Insert iNum at position-1 – numbers[position-1] = iNum;
- Increment size – size++;
- Display the resultant array.
Insert Element in Array in C Program:
Here is the program to insert an element in an array in the c programming language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
/* Program to insert element in array Author: SillyCodes.com */ #include <stdio.h> /** * @brief - Read the user input and update the 'numbers' array * * @param numbers - 'numbers' array * @param size - size of the 'numbers' array */ void read(int numbers[], int size) { int i; // User input for array elements printf("Please enter array elements(%d) : ", size); for(i = 0; i < size; i++) { // printf("numbers[%d] : ", i); scanf("%d", &numbers[i]); } } /** * @brief - Display the 'numbers' array * * @param numbers - Array * @param size - Array size */ void display(int numbers[], int size) { int i; for(i = 0; i < size; i++) { printf("%d ", numbers[i]); } printf("\n"); } int main() { // declare the 'numbers' array int numbers[1000]; int i, size, iNum, position; printf("Enter desired array size(1-1000): "); scanf("%d", &size); // User input for array elements read(numbers, size); // Take the position and number to inserted from the user printf("Enter the position(1-%d) : ", size); scanf("%d", &position); printf("Enter the value to insert: "); scanf("%d", &iNum); // check for array bounds if(position>size || position<1) { printf("Invalid Position, Position should be 1-%d, Try again \n", size); return 0; } // Print the original array printf("Original Array: "); display(numbers, size); // Move all elements from 'position' to right side. // so start from the 'size' index and move each element to right. for(i = size; i>= position; i--) { // moving 'i-1' to 'i' numbers[i] = numbers[i-1]; } // insert the 'iNum' at 'position-1' numbers[position-1] = iNum; // increment the size size++; // Print the sorted array printf("Array after inserting element: "); display(numbers, size); return 0; } |
Program Output:
Compile the program using GCC compiler(any compiler)
gcc insert-ele-arr.c
Run the executable file.
Test Case 1: Positive Cases:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$ ./a.out Enter desired array size(1-1000): 9 Please enter array elements(9) : 11 22 33 44 66 77 88 99 110 Enter the position(1-9) : 5 Enter the value to insert: 55 Original Array: 11 22 33 44 66 77 88 99 110 Array after inserting element: 11 22 33 44 55 66 77 88 99 110 $ ./a.out Enter desired array size(1-1000): 5 Please enter array elements(5) : 1 2 3 4 5 Enter the position(1-5) : 5 Enter the value to insert: 1000 Original Array: 1 2 3 4 5 Array after inserting element: 1 2 3 4 1000 5 $ ./a.out Enter desired array size(1-1000): 7 Please enter array elements(7) : 12 34 87 64 73 98 23 Enter the position(1-7) : 1 Enter the value to insert: 5 Original Array: 12 34 87 64 73 98 23 Array after inserting element: 5 12 34 87 64 73 98 23 $ |
As we can see from the above output, The program is generating the desired results.
For example, The given array is 1 2 3 4 5 and we want to insert an element 1000 at the position 5 (Note, position = index + 1), Then the resultant array is 1 2 3 4 1000 5.
Test case 2: Negative case:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//Negative test cases $ ./a.out Enter desired array size(1-1000): 5 Please enter array elements(5) : 1 2 3 4 5 Enter the position(1-5) : 0 Enter the value to insert: 100 Invalid Position, Position should be 1-5, Try again $ ./a.out Enter desired array size(1-1000): 5 Please enter array elements(5) : 9 8 7 6 5 Enter the position(1-5) : 6 Enter the value to insert: 434 Invalid Position, Position should be 1-5, Try again $ ./a.out Enter desired array size(1-1000): 5 Please enter array elements(5) : 1 2 3 4 5 Enter the position(1-5) : 5 Enter the value to insert: 600 Original Array: 1 2 3 4 5 Array after inserting element: 1 2 3 4 600 5 $ |
If the user enters a position, which is out of array bounds, Then we will display an error message – Invalid Position.
Insert an element in an Array using a user-defined function in C language:
Let’s move the insert element logic to a user-defined function named insertAt. So that we can insert as many elements as by just calling the insertAt function.
Here is the rewritten version of the above program, Where we defined a new function insertAt, Which takes four formal arguments and inserts the given number iNum at the specified position in the numbers[] array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
/* Program to insert element in array Author: SillyCodes.com */ #include <stdio.h> int insertAt(int numbers[], int size, int position, int iNum); /** * @brief - Read the user input and update the 'numbers' array * * @param numbers - 'numbers' array * @param size - size of the 'numbers' array */ void read(int numbers[], int size) { int i; // User input for array elements printf("Please enter array elements(%d) : ", size); for(i = 0; i < size; i++) { // printf("numbers[%d] : ", i); scanf("%d", &numbers[i]); } } /** * @brief - Display the 'numbers' array * * @param numbers - Array * @param size - Array size */ void display(int numbers[], int size) { int i; for(i = 0; i < size; i++) { printf("%d ", numbers[i]); } printf("\n"); } int main() { // declare the 'numbers' array int numbers[1000]; int i, size, iNum, position; printf("Enter desired array size(1-1000): "); scanf("%d", &size); // User input for array elements read(numbers, size); // Take the position and number to inserted from the user printf("Enter the position(1-%d) : ", size); scanf("%d", &position); printf("Enter the value to insert: "); scanf("%d", &iNum); // check for array bounds if(position>size || position<1) { printf("Invalid Position, Position should be 1-%d, Try again \n", size); return 0; } // Print the original array printf("Original Array: "); display(numbers, size); // Call the 'insertAt' function size = insertAt(numbers, size, position, iNum); // Print the sorted array printf("Array after inserting element: "); display(numbers, size); return 0; } /** * @brief - Insert 'iNum' at 'position' in 'numbers' array * * @param numbers - Array * @param size - Array size * @param position - position to insert the element * @param iNum - number to be inserted * @return int - Updated size (should size++) */ int insertAt(int numbers[], int size, int position, int iNum) { int i; // Move all elements from 'position' to right side. // so start from the 'size' index and move each element to right. for(i = size; i>= position; i--) { // moving 'i-1' to 'i' numbers[i] = numbers[i-1]; } // insert the 'iNum' at 'position-1' numbers[position-1] = iNum; // increment the size size++; return size; } |
The insertAt function:
- The prototype of the insertAt function is int insertAt(int numbers[], int size, int position, int iNum)
-
insertAt function takes four formal arguments
- numbers[] array,
- size of the array
- position to insert new value
- iNum the value to be inserted.
- The insertAt function inserts the iNum at the position in the numbers array of size size.
Program Output:
Let’s compile and Run the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$ ./a.out Enter desired array size(1-1000): 4 Please enter array elements(4) : 73 45 23 98 Enter the position(1-4) : 2 Enter the value to insert: 431 Original Array: 73 45 23 98 Array after inserting element: 73 431 45 23 98 $ ./a.out Enter desired array size(1-1000): 10 Please enter array elements(10) : 12 34 45 56 78 90 24 68 80 20 Enter the position(1-10) : 5 Enter the value to insert: 676 Original Array: 12 34 45 56 78 90 24 68 80 20 Array after inserting element: 12 34 45 56 676 78 90 24 68 80 20 $ |
and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Negative testing $ ./a.out Enter desired array size(1-1000): 5 Please enter array elements(5) : 10 20 30 40 50 Enter the position(1-5) : 0 Enter the value to insert: 900 Invalid Position, Position should be 1-5, Try again $ ./a.out Enter desired array size(1-1000): 6 Please enter array elements(6) : 1 2 3 4 5 6 Enter the position(1-6) : 8 Enter the value to insert: 12 Invalid Position, Position should be 1-6, Try again $ |
Related Array Programs:
- Index – C Language Practice Programs
- C Tutorials Index – Step by step tutorials to master C Langauge
- C Program to Read and Print Arrays
- Reverse Print Array Program in C
- C Program to calculate the total number of Positive numbers, Negative numbers, and Zeros in an Array
- C Program to Count Even and Odd numbers in Array
- C Program to Find Maximum Element in Array
- C Program to Reverse the Array Elements
- C Program to Reverse Array Elements using Recursion
- C Program to Sort Array Elements in Ascending order
- C Program to Sort Array in Ascending and Descending Order using QSort function
4 Responses
[…] our earlier article, We have looked at the program to Insert an element in an array and In today’s article, we will write a Program to Delete Element from Array in C Langauge. The […]
[…] Insert an element in an Array […]
[…] C Program to Insert an element in Array […]
[…] C Program to Insert an element in Array […]