Algorithm For Inserting an Element in The Array With Program

Errorlogger
0

Insert (LA, N, K, Item)

In this we have array LA specify the Linear Array with N element, K specify the location where we want to insert the value of Item variable.

Algorithm for Inserting an Element in The Array

  1. Set J: = N
  2. Repeat steps 3 and 4 While J >= K 
  3. Set LA[J + 1]: = LA[J]
  4. Set J: = J - 1 (End of step 2 Loop)
  5. Set LA[K]: = item
  6. Set N: = N + 1
  7. Exit 

Write a program to perform insertion operation from the array.

#include <stdio.h>
void main()
{
      int a[10], i, n, val, k;
      printf("Enter the range of the array = ");
      scanf("%d", &n);
      printf("Enter array's element's = ");
      for(i = 0; i < n; i ++)
      {
             scanf("%d", &a[i]);
      }
      printf("Enter the element you want to insert = ");
      scanf("%d", &val);
      printf("Enter the location = ");
      scanf("%d", &k);
      for(i = n - 1; i >= k; i --)
      {
             a[i + 1] = a[i];
      }
      a[k] = val;
      n = n + 1;
      printf("Array's element's = ");
      for(i = 0; i < n; i ++)
      {
             scanf("%d\t", a[i]);
      }
}

Insertion:- It is the process in which we add a new element in the array the element will be inserted at the specific position to insert the element in the array first of all we have to create the space for i.e  that element for this purpose we have to move all the elements in forward direction. We will move the elements from last position to that location in which we want to insert the element. After moving these element at given location. After that the size of the array will increase by one.

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(30)

Our website uses cookies to enhance your experience. Check Now
Accept !