Write a program in C++ to insert an element 2 in a given array 1 3 5 at a Position 2
#include<iostream>
using namespace std;
int main()
{
int arr[4], i,
elem, pos, tot;
cout<<"Enter the Size for Array: \n";
cin>>tot;
cout<<"Enter "<<tot<<" Array Elements:
\n";
for(i=0; i<tot;
i++)
cin>>arr[i];
cout<<"Enter Element to Insert: \n";
cin>>elem;
cout<<"At What Position ? ";
cin>>pos;
for(i=tot;
i>=pos; i--)
arr[i] =
arr[i-1];
arr[i] = elem;
tot++;
cout<<"\nThe New Array is: ";
for(i=0; i<tot;
i++)
cout<<arr[i]<<"
";
cout<<endl;
return 0;
}
Output -
Enter the Size for Array: 3
Enter 3 Array Elements: 1 3 5
Enter Element to Insert: 2
At What Position ? 2
The New Array is: 1 2 3 5
Note - Orange colour text represent input is entered by user
0 Comments