-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathinsertion.cpp
More file actions
40 lines (30 loc) · 755 Bytes
/
insertion.cpp
File metadata and controls
40 lines (30 loc) · 755 Bytes
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
// C++ Program to Insert an element at a specific position in an Array
#include<iostream>
using namespace std;
// Function to insert element at a specific position
void insertElement(int arr[], int n, int x, int pos)
{
// shift elements to the right
// which are on the right side of pos
for (int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];
arr[pos] = x;
}
// Driver's code
int main()
{
int arr[15] = { 2, 4, 1, 8, 5 };
int n = 5;
cout<<"Before insertion : ";
for (int i = 0; i < n; i++)
cout<<arr[i]<<" ";
cout<<endl;
int x = 10, pos = 2;
// Function call
insertElement(arr, n, x, pos);
n++;
cout<<"After insertion : ";
for (int i = 0; i < n; i++)
cout<<arr[i]<<" ";
return 0;
}