Here is a C++program to insert an element at nth position in an array
SOURCE CODE OUTPUT
#include<iostream>
using namespace std;
int main()
{
int a[20],num,n,element;
cout<<"How many elements to be stored (max 20) : ";
cin>>num;
cout<<"Enter elements ";
for(int i=0;i<num;i++)
cin>>a[i];
cout<<"Enter element to be inserted ";
cin>>element;
cout<<"Enter value of n : ";
cin>>n;
for(int i=num-1;i>=n-1;i--)
a[i+1]=a[i];
a[n-1]=element;
cout<<"New array : ";
for(int i=0;i<=num;i++)
cout<<a[i]<<" ";
return 0;
}
using namespace std;
int main()
{
int a[20],num,n,element;
cout<<"How many elements to be stored (max 20) : ";
cin>>num;
cout<<"Enter elements ";
for(int i=0;i<num;i++)
cin>>a[i];
cout<<"Enter element to be inserted ";
cin>>element;
cout<<"Enter value of n : ";
cin>>n;
for(int i=num-1;i>=n-1;i--)
a[i+1]=a[i];
a[n-1]=element;
cout<<"New array : ";
for(int i=0;i<=num;i++)
cout<<a[i]<<" ";
return 0;
}
Any questions regarding to program please write in comments.
No comments:
Post a Comment