-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrinsert.c
More file actions
37 lines (33 loc) · 738 Bytes
/
arrinsert.c
File metadata and controls
37 lines (33 loc) · 738 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
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Enter size of array: ");
scanf("%d", &n);
int* arr = malloc(n * sizeof(int));
for(int i = 0; i < n; i++)
{
printf("> ");
scanf("%d", &arr[i]);
}
printf("Current array: ");
for(int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
int value, pos;
printf("Enter value to insert and position: ");
scanf("%d %d", &value, &pos);
arr = realloc(arr, (n + 1) * sizeof(int));
for(int i = n - 1; i >= pos - 1; i--)
{
arr[i + 1] = arr[i];
}
arr[pos - 1] = value;
printf("New array: ");
for(int i = 0; i < n + 1; i++)
{
printf("%d ", arr[i]);
}
}