-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.c
More file actions
58 lines (58 loc) · 1.42 KB
/
36.c
File metadata and controls
58 lines (58 loc) · 1.42 KB
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
#include <stdio.h>
int main()
{
int arr[100], n, i, element, pos, after_value;
char choice;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter array elements: ");
for(i = 0; i < n; i++) scanf("%d", &arr[i]);
printf("Original array: ");
for(i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
printf("Insert by (p)osition or (v)alue? ");
scanf(" %c", &choice);
if(choice == 'p')
{
printf("Enter element to insert: ");
scanf("%d", &element);
printf("Enter position (0-based index): ");
scanf("%d", &pos);
if(pos >= 0 && pos <= n)
{
for(i = n; i > pos; i--) arr[i] = arr[i-1];
arr[pos] = element;
n++;
}
else
{
printf("Invalid position!\n");
}
}
else if(choice == 'v')
{
printf("Enter element to insert: ");
scanf("%d", &element);
printf("Enter value after which to insert: ");
scanf("%d", &after_value);
int found = 0;
for(i = 0; i < n; i++)
{
if(arr[i] == after_value)
{
for(int j = n; j > i+1; j--) arr[j] = arr[j-1];
arr[i+1] = element;
n++;
found = 1;
break;
}
}
if(!found) printf("%d not found in array.\n", after_value);
} else {
printf("Invalid choice!\n");
}
printf("Array after insertion: ");
for(i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}