Skip to content

Commit aaba68f

Browse files
authored
Update Array.md
1 parent 461ca12 commit aaba68f

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

Array.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,43 @@ int main()
8484
### Insertion
8585
Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array.
8686

87-
Here, we see a practical implementation of insertion operation, where we add data at the end of the array −
87+
Here, we see a practical implementation of insertion operation, where we add data at the given position of the array −
8888

8989
Example
9090

9191
Following is the implementation of the above algorithm −
9292

93+
```c
94+
#include<stdio.h>
95+
96+
int main()
97+
98+
{
99+
int array[5] = { 2, 6, 8, 3, 9};
100+
101+
// initial size of array
102+
int n = 5;
103+
int i = 0;
104+
105+
// element to be inserted
106+
int x = 4;
107+
108+
// position at which the element has to be inserted
109+
int pos = 3;
110+
111+
// increase the size by 1
112+
n++;
113+
114+
// shift elements after the pos forward
115+
for (i = n - 1; i >= pos; i--)
116+
array[i] = array[i-1];
117+
118+
// insert x at pos
119+
array[pos-1] = x;
120+
121+
// print new array
122+
for (i = 0; i < n; i++)
123+
printf("%d ",array[i]);
124+
125+
}
126+
```

0 commit comments

Comments
 (0)