Insert an element into an array at a specified position

Problem statement:- Program to Insert an element into an array at a specified position.

Data requirement:-

   Input Data:- arr,size, pos, ele

  Output Data:-arr

  Additional Data:- i, j

Program in C

Here is the source code of the C Program to Insert an element into an array at a specified position.

Code:

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

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

    int ele;
    printf("\nEnter the element:");
    scanf("%d",&ele);
    printf("Enter the position:");
    int pos;
    scanf("%d",&pos);
    printf("\nBefore inserting array elements are:");
    for(i=0;i<size;i++)
    {
        printf("%d ",arr[i]);
    }
    size++;
    for(i=size-1;i>=pos-1;i--)
        arr[i+1]=arr[i];
    arr[pos-1]=ele;
    printf("\nAfter inserting array elements are:");
    for(i=0;i<size;i++)
    {
        printf("%d ",arr[i]);
    }
}

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
1
2
3
5
6

Enter the element:4
Enter the position:4

Before inserting array elements are:1 2 3 5 6
After inserting array elements are:1 2 3 4 5 6

Program in C++

Here is the source code of the C++ Program to Insert an element into an array at a specified position.

Code:

#include<iostream>
using namespace std;
int main()
{

    cout<<"Enter the size of the array:";
    int size;
    cin>>size;
    int arr[size];
    int i;
    cout<<"Enter the Element of the array:\n";
    for(i=0;i<size;i++)
    {
        cin>>arr[i];
    }

    int ele;
    cout<<"\nEnter the element:";
    cin>>ele;
    cout<<"Enter the position:";
    int pos;
    cin>>pos;

    cout<<"\nBefore inserting array elements are:";
    for(i=0;i<size;i++)
    {
        cout<<arr[i]<<" ";
    }
    size++;
    for(i=size-1;i>=pos-1;i--)
        arr[i+1]=arr[i];
    arr[pos-1]=ele;

    cout<<"\nAfter inserting array elements are:";
    for(i=0;i<size;i++)
    {
        cout<<arr[i]<<" ";
    }
}

Input/Output:
Enter the size of the array:4
Enter the Element of the array:
10
12
13
14

Enter the element:9
Enter the position:1

Before inserting array elements are:10 12 13 14
After inserting array elements are:9 10 12 13 14

Program in Java

Here is the source code of the Java Program to Insert an element into an array at a specified position.

Code:

import java.util.Scanner;
public class p21 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
    int size,i;
    size=sc.nextInt();
    int arr[ ]=new int[size+1];
System.out.println("Enter the Element of the array:");
     for(i=0;i<size;i++)
    {
        arr[i]=sc.nextInt();
    }
     int ele;
     System.out.print("\nEnter the element:");
     ele=sc.nextInt();
     System.out.print("Enter the position:");
     int pos;
     pos=sc.nextInt();
     System.out.print("\nBefore inserting array elements are:");
     for(i=0;i<size;i++)
     {
    System.out.print(arr[i]+" ");
     }
     for(i=size-1;i>=pos-1;i--)
         arr[i+1]=arr[i];
     arr[pos-1]=ele;
     System.out.print("\nAfter inserting array elements are:");
     for(i=0;i<size+1;i++)
     {
    System.out.print(arr[i]+" ");
     }
     sc.close();
}
}

Input/Output:
Enter the size of the array:
5
Enter the Element of the array:
4
3
2
1
6

Enter the element:5
Enter the position:6

Before inserting array elements are:4 3 2 1 6 
After inserting array elements are:4 3 2 1 6 5 

Program in Python

Here is the source code of the Python Program to Insert an element into an array at a specified position.

Code:

arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)
print("Enter the element:")
ele=int(input())
print("Enter the position:")
pos=int(input())
print("Before inserting array elements are:")
for i in range(0,size):
    print(arr[i],end=" ")
arr.insert(pos-1,ele)
print("\nAfter inserting array elements are:")
print(arr)

Input/Output:
Enter the size of the array: 5
Enter the Element of the array:
1
2
3
4
5
Enter the element:
9
Enter the position:
3
Before inserting array elements are:
1 2 3 4 5 
After inserting array elements are:
[1, 2, 9, 3, 4, 5]

Post a Comment

0 Comments