-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathinserting_element.cpp
More file actions
70 lines (58 loc) · 1.5 KB
/
Copy pathinserting_element.cpp
File metadata and controls
70 lines (58 loc) · 1.5 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
using namespace std;
int main()
{
int x;
int arr[50], i, num,ele, pos;
cout << "Enter the no of elements u want in the array :" << endl;
cin >>ele;
cout << "Enter the elements:"<<endl;
for (int i = 0; i<ele; i++)
{
cin>>arr[i];
}
cout << "The array is: [";
for (int i = 0; i <ele; i++)
{
cout<<arr[i] << " ";
}
cout<<endl;
cout << "Chosen from the given option" <<endl;
cout << "1 for inserting at the specific position"<<endl<<"2 for inserting at beginning "<<endl;
cout << "Enter ur choice:"<<endl;
cin >> x;
switch (x)
{
case 1:
cout << "Enter the postion at which u want to insert: " <<endl;
cin >> pos;
cout << "Enter an element u want to insert: " <<endl;
cin >> num;
for (i=ele; i >= pos; i--)
{
arr[i] = arr[i - 1];
}
arr[i] = num;
cout << "The new array is : [";
for (i = 0; i <=ele; i++)
{
cout << arr[i]<<" ";
}
break;
case 2:
cout << "Enter an element u want to insert: ";
cin >> num;
for (i = ele- 1; i >= 0; i--)
{
arr[i + 1] = arr[i];
}
arr[0] = num;
cout << "The new array is : [";
for (i = 0; i <= ele; i++)
{
cout <<arr[i]<<' ';
}
break;
}
return 0;
}