forked from abhijithshaji17/cpp-mastery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31.cpp
More file actions
38 lines (38 loc) · 1.04 KB
/
Copy path31.cpp
File metadata and controls
38 lines (38 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// insertion operation on array straight - position
#include<iostream>
using namespace std;
int main(){
int a[20],i,ub,lb,val,pos;
// make n print array
cout<<"Enter the lower and upper bounds of array: ";
cin>>lb>>ub;
cout<<"Enter elements:\n";
for(i=lb;i<ub;i++){
cout<<"Element "<<i+1<<": ";
cin>>a[i];
}
cout<<"Array:\n";
cout<<"[";
for(i=lb;i<ub;i++){
cout<<" "<<a[i];
}
cout<<"]\n";
// insert value at position
cout<<"\nEnter value to insert: ";
cin>>val;
cout<<"\nEnter position(index): ";
cin>>pos;
for(i=ub-1;i>=pos;i--){ // Shift existing elements one position to the right
a[i+1] = a[i];
}
a[pos] = val; // Insert new value at the desired position
ub++; // Increment upper bound once after shifting is done
// array after inserting
cout<<"\nNew Array:\n";
cout<<"[";
for(i=lb;i<ub;i++){
cout<<" "<<a[i];
}
cout<<"]";
return 0;
}