Insert the element in an array

amannarayanm · September 29, 2021 · C++
#include <iostream>
using namespace std;

void traversal(int arr[],int size)
{
    for(int i=0;i<size;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

int insertion(int arr[],int size,int element,int index,int capacity)
{
    if(size>=capacity)
    {
        return -1;
    }
    else
    {
        for(int i=size-1;i<=index;i--)
        {
            arr[i+1] = arr[i];
        }
        arr[index]=element;   
    }
    return 1;
}


int main() {
    int arr[50]= {12,13,14,15,16};
    int size=5,element = 100;
    traversal(arr,size);
    int result = insertion(arr,size,element,3,50);
    if(result==1)
    {
        size=size+1;
        traversal(arr,size);   
    }
    else
    {
        cout<<"Insertion is faild";
    }
    return 0;
}






































Comments

Please sign up or log in to contribute to the discussion.