-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharray.c
More file actions
85 lines (72 loc) · 2.25 KB
/
array.c
File metadata and controls
85 lines (72 loc) · 2.25 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
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
#include<stdio.h>
int main()
{
int i,a[i],n,c,e,position;
printf("Enter the number of elements in the array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the elements of the array:");
scanf("%d",&a[i]);
}
do{
printf("Enter your choice:\n");
printf("1.Traversal\n");
printf("2.Insertion\n");
printf("3.Deletion\n");
printf("4.Updation\n");
printf("\n");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:printf("Array\n");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
break;
case 2: printf("Enter element to insert: ");
scanf("%d", &e);
printf("Enter position: ");
scanf("%d", &position);
if (position < 0 || position > n) {
printf("Index out of bounds.\n");
} else {
for (i = n; i > position; i--) {
a[i] = a[i - 1];
}
a[position] = e;
n++;
printf("Inserted %d at index %d.\n", e, position);
}
break;
case 3: printf("Enter position to delete from: ");
scanf("%d", &position);
if (position < 0 || position >= n) {
printf("Position out of bounds.\n");
} else {
int deletedElement = a[position];
for (i = position; i < n - 1; i++) {
a[i] = a[i + 1];
}
n--;
}
break;
case 4: printf("Enter position to update: ");
scanf("%d", &position);
printf("Enter new element: ");
scanf("%d", &e);
if (position < 0 || position >= n) {
printf("Index out of bounds.\n");
} else {
a[position] = e;
printf("Updated index %d with %d.\n", position, e);
}
break;
default:
printf("Invalid choice, please try again.\n");
}
}while(c<=4);
return 0;
}