-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_at_index_array.cpp
44 lines (41 loc) · 1.01 KB
/
insert_at_index_array.cpp
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
#include <iostream>
using namespace std;
void insertAtIndex(int arr[], int sizeOfArray, int index, int element) {
int prevTemp;
for(int i=0; i <= sizeOfArray; i++){
if(i >= index) {
int currTemp;
if(i == index){
prevTemp = arr[i];
arr[i] = element;
} else {
currTemp = arr[i];
arr[i] = prevTemp;
prevTemp = currTemp;
}
}
}
}
int main() {
int t;
int indexForElement;
int element;
cout << "Enter array size\n";
cin >> t;
int arr[t];
int index = 0;
cout << "Enter array element one by one\n";
while (index < t) {
cin >> arr[index];
index++;
}
cout << "Enter index where to place an element\n";
cin >> indexForElement;
cout << "Enter element to place at the provided index\n";
cin >> element;
insertAtIndex(arr, t, indexForElement, element);
for (int x : arr)
{
cout << x << " ";
}
}