-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion.c
More file actions
49 lines (44 loc) · 868 Bytes
/
insertion.c
File metadata and controls
49 lines (44 loc) · 868 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
41
42
43
44
45
46
47
48
49
//traversing the array first
#include<stdio.h>
int main()
{
int i,n, array[50];
printf("Enter size : ");
scanf("%d",&n);
printf("Plese enter array elements : \n ");
for (i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
int pos,num;
printf("at which position do you want to insert?: ");
scanf("%d",&pos);
//manualy checking bound
if (pos>n+1/*you can also enter at nth postion */ || pos<0)
//there might be a bug for if (pos >= n || pos < 0)
//i will deal with it later
{
printf("This area is out of bound , please enter valid range ");
}
else
{
printf("Enter num:");
scanf("%d",&num);
for(i=n-1;i>=pos-1;i--)
{
array[i+1] = array[i];
}
array[pos-1]=num;
n++;
// also you could use
// for (int i = n; i > pos; i--) {
// array[i] = array[i - 1];
// }
// array[pos] = num;
// n++;
for(i=0;i<n;i++)
{
printf("%d ",array[i]);
}
}
}