-
Notifications
You must be signed in to change notification settings - Fork 0
/
array-insertion
93 lines (47 loc) · 836 Bytes
/
array-insertion
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<stdio.h>
void read(int a[],int n)
{
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void display(int a[],int n)
{
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
void insertAtPosition(int a[],int n, int pos, int value)
{
for(int i=n;i>pos-1;i--)
{
a[i]=a[i-1];
}
a[pos-1]=value;
}
int main()
{
int arr[50], n, pos, c, value;
printf("Enter the number of elements in array : ");
scanf("%d", &n);
printf("Enter %d elements : ", n);
read(arr, n);
printf("Enter the position with in %d : ", n);
scanf("%d", &pos);
if (pos<=n)
{
scanf("%d", &value);
insertAtPosition(arr, n, pos, value);
n++;
printf("The resultant array : ");
display(arr, n);
}
else
{
printf("Enter the correct available position \n");
}
return 0;
}