Input & Output Format:
Assume that the maximum number of elements in the array is 20.
Sample Input:
Enter the number of elements in the array
5
Enter the elements in the array
1
2
3
4
5
Enter the location where you wish to insert an element
4
Enter the value to insert
10
Sample Output:
Array after insertion is
1
2
3
10
4
5
SOLUTION:
#include<iostream>
using namespace std;
int main()
{
int E, i, l, v;
cout<<"Enter the number of elements in the array"<<endl;;
cin>>E;
int a[E+1];
cout<<"Enter the elements in the array"<<endl;;
for(i = 0; i < E; i++){
cin>>a[i];
}
cout<<"Enter the location where you wish to insert an element"<<endl;;
cin>>l;
if(l > E){
cout<<"Invalid Input"<<endl;;
}
else{
cout<<"Enter the value to insert"<<endl;;
cin>>v;
for(i = E; i > l-1; i--){
a[i] = a[i-1];
}
a[l-1] = v;
cout<<"Array after insertion is"<<endl;
for(i = 0; i <= E; i++){
cout<<a[i]<<endl;;
}
}
}
No comments:
Post a Comment