Insert Element in Array in C Language

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

inserting-number-in-array-example

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

Insert Element in Array in C Explanation:

  1. 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.
  2. Take the desired array size from the user and store it in size array.
  3. 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
  4. Call the read() function from the main() function to update the array elements.
  5. 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.
  6. Also, take the number to be inserted from the user and store it in the variable iNum.
  7. 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.
  8. 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.
  9. Once the above loop is completed, Insert the given number ( iNum) at the position-1 index. – numbers[position-1] = iNum;
  10. Increment the array size by 1. (As we inserted a new element to the array)
  11. Display the Array after insertion on the console by calling the display() function.

Algorithm to Insert Element in Array in C:

  1. Declare numbers[1000] array and i, size, iNum, and position variables
  2. Take the array size from the user – scanf("%d", &size);
  3. Call read() function to update the array with user input – read(numbers, size);
  4. Take the position and iNum from the user
  5. Display the original array by calling display() function – display(numbers, size);
  6. For Loop : start from <strong> i=size</strong> to i>=position
    • numbers[i] = numbers[i-1];
    • i--;
  7. Insert iNum at position-1numbers[position-1] = iNum;
  8. Increment size – size++;
  9. 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.

Program Output:

Compile the program using GCC compiler(any compiler)

gcc insert-ele-arr.c

Run the executable file.

Test Case 1: Positive Cases:

insert-element-in-array-in-c-program-output

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:

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.

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.

insert-element-in-an-array-using-function-in-c

and

Related Array Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

4 Responses

  1. […] 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 […]

  2. […] Insert an element in an Array […]

  3. […] C Program to Insert an element in Array […]

  4. […] C Program to Insert an element in Array […]

Leave a Reply