C Program to insert a element in specified position in array


February 19, 2022, Learn eTutorial
1272

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

How did an array works in C?

In this c example, we will discuss the method to insert an element in an array at a specified position. We usually know array adds the elements in sequential order from position 1 to n. In this c program at first, we receive the number of elements and the elements from the user. Add the input into the array; we need to sort the array in ascending order to add the element in the correct position.

How to sort and insert an element in an array?

By using an outer and inner for loop(nested loop), we sort the elements in the array. Now we accept the key elements from the user to insert in our sorted array. Check the key is less than the first element in the array if so add the key in the first position. If not make "m = n - pos + 1" and use a for loop up to "m" and inside loop apply "x[n-i+2] = x[n-i+1]" to get the position to add the key x[pos] = key. Display the array with the key element in the correct position.

The logic of this program is to declare an array and read the elements into it. We were then using a for loop to sort the numbers in the array. Then we display the sorted list. Finally, we will check the position for inserting the given element and place the element into that position and display the array.

ALGORITHM

STEP 1: Include the header files to use the built-in functions in the C program.

STEP 2: Declare the Array x[10] and some integer variables I, j, n, m, Key, and pos.

STEP 3: Accept the number of elements into 'n'.

STEP 4: Read the elements into the Array x[i] using For loop.

STEP 5: Display Array elements using for loop.

STEP 6: Sort the Array using the inner for loop.

STEP 7: Then display the Sorted Array as the x[i] using for loop.

STEP 8: Read the number t into the variable Key.

STEP 9: By using a for loop check key.

STEP 10: Increment 'i' by 1 and do STEP 9.

STEP 11: m = n-pos+1.

STEP 12: Using a for loop with the condition 'i<=m' do step 13.

STEP 13:  x[n-i+2] = x[n-i+1]

STEP 14: Then place the number in the position x[pos]=key.

STEP 15: Then display the final list as x[i[] using for loop.

C Source Code

                                          #include <stdio.h>

void main()
{
  int x[10];
  int i, j, n, m, temp, key, pos;
  printf("Enter how many elements\n"); /* gets the elements */
  scanf("%d", &n);
  printf("Enter the elements\n");
    for (i = 0; i < n; i++)
        {
            scanf("%ld", & x[i]);
            fflush(stdin);
        }
  printf("Input array elements are\n");
  for (i = 0; i < n; i++)
  {
    printf("%d\n", x[i]);
  }

  for (i = 0; i < n; i++)
  {
    for (j = i + 1; j < n; j++) /* sort the array to add the key number in correct position */
    {
      if (x[i] > x[j])
      {
        temp = x[i];
        x[i] = x[j];
        x[j] = temp;
      }
    }
  }
  printf("Sorted list is\n");
   for (i = 0; i < n; i++)
    {
        printf("%d\n", x[i]);
    }
  printf("Enter the element to be inserted \n");
    scanf("%d", & key);        
  for (i = 0; i < n; i++)
  {
    if (key < x[i]) /* checking key is less than the first element of array */
    {
      pos = i;
      break;
    }
  }
  m = n - pos + 1;
  for (i = 0; i <= m; i++)
  {
    x[n - i + 2] = x[n - i + 1]; /* add the key number in the correct position */
  }
  x[pos] = key;
  printf("Final list is\n");
  for (i = 0; i < n + 1; i++)
  {
    printf("%d\n", x[i]); /* display the output array with the key number added  */
  }
} /* End of main() */
                                      

OUTPUT

Enter how many elements

5

Enter the elements

2

14

67

83

29

Input array elements are

2

14

67

83

29

Sorted list is

2

14

29

67

83

Enter the element to be inserted

34

Final list is

2

14

29

34

67

83