Program to insert a number at given position of an Array

Description

* An element can only be inserted if memory space allocated for array is still available.
* To add number at the end just increment upper bound to 1 and assign the value.
* If insert an element at particular position, increment the index of all elements by 1 
  which is right to position of element to be inserted then insert element at that position.

C/C++

/* C program to insert element at a given location*/
//Save it as InsertElementGivenLocation.c


#include<stdio.h>
int main(){

    int i, n, pos, num;

    
    /* Declare array of larger size so that memory will be 
     available for element to be inserted */
    
    int arr[100];

    printf("Enter the size of array : ");
    scanf("%d",&n);

    printf("Enter the elements of the array : ");
    for (i = 0; i < n; i++) {
        scanf("%d",&arr[i]);
    }

    printf("Enter the position where elements to be entered : ");
    scanf("%d",&pos);

    printf("Enter the element to be entered : ");
    scanf("%d",&num);

    
    /* Execute a loop to move all elements right by 1 position having 
    index greater than position where to insert element */
    
    for(i=n-1;i>=pos;i--) {
        arr[i+1] = arr[i];
    }

    // Insert element at given position
    arr[pos] = num;

    // Finally print new array after insertion of new element
    printf("The new array is : ");
    for(i=0;i<=n;i++) {
        printf("%d ", arr[i]);
    }
    
    return 0;
}
Input:
Enter the size of array : 
4
Enter the elements of the array : 
6
4
2
8
Enter the position where elements to be entered : 
2
Enter the element to be entered : 
7

Output:
The new array is : 
6 4 7 2 8

Java

/*Java program to insert element at a given location*/
//Save it as InsertElementGivenLocation.java

import java.io.*;
import java.util.Scanner;

public class InsertElementGivenLocation {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        
        /* Declare array of larger size so that memory will be 
           available for element to be inserted */
        
        int arr[] = new int[100];

        System.out.println("Enter the size of array : ");
        int n = scanner.nextInt();

        System.out.println("Enter the elements of the array : ");
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("Enter the position where elements to be entered : ");
        int pos = scanner.nextInt();
        
        System.out.println("Enter the element to be entered : ");
        int num = scanner.nextInt();
        
        
        /* Execute a loop to move all elements right by 1 position having 
           index greater than position where to insert element */
        
        for(int i=n-1;i>=pos;i--) {
            arr[i+1] = arr[i];
        }
        
        // Insert element at given position
        arr[pos] = num;
        
        // Finally print new array after insertion of new element
        System.out.println("The new array is : ");
        for(int i=0;i<=n;i++) {
            System.out.print(arr[i]+" ");
        }
    }
}
Input:
Enter the size of array : 
4
Enter the elements of the array : 
6
4
2
8
Enter the position where elements to be entered : 
2
Enter the element to be entered : 
7

Output:
The new array is : 
6 4 7 2 8

Related Programs

1) Insert a number in an array sorted in ascending order
2) Program to delete an element from given location in an array
3) Program to delete an element from an array sorted in ascending order
4) Program to merge two sorted arrays
5) Program to swap maximum and minimum element of Array
6) Program to Rearrange an array such that arr[i]=i
7) Program to Find the smallest missing number
8) Program to Find K largest elements from array
9) Program to make pair of elements alternatively
10) Remove Duplicate Elements From Array
Share Me

Leave a Reply