-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_op.c
112 lines (107 loc) · 2.36 KB
/
array_op.c
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <stdlib.h>
int a[100], n, i, pos;
void create()
{
printf("Enter the no. of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements in array\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
}
void display()
{
printf("The Array Contents are :\n");
for (i = 0; i < n; i++)
{
printf("a[%d]=%d\n", i, a[i]);
}
}
void insert()
{
int ele;
printf("enter the element to be inserted and its position\n");
scanf("%d%d", &ele, &pos);
if (pos > n || pos < 0)
{
printf("Invalid Position\n");
return;
}
for (i = n - 1; i >= pos; i--)
{
a[i + 1] = a[i];
}
a[pos] = ele;
n = n + 1;
printf("\nArray after Insertion\n");
for (i = 0; i < n; i++)
{
printf("a[%d]=%d\n", i, a[i]);
}
return;
}
void del()
{
printf("Enter the position to delete element:\t");
scanf("%d", &pos);
if (pos < 0 || pos >= n)
{
printf("Invalid Position\n");
return;
}
printf("Element deleted is %d\n", a[pos]);
for (i = pos + 1; i < n; i++)
{
a[i - 1] = a[i];
}
n = n - 1;
printf("\nArray after Deletion\n");
for (i = 0; i < n; i++)
{
printf("a[%d]=%d\n", i, a[i]);
}
}
void main()
{
int ch;
for (;;)
{
printf("\nEnter 1 : create array");
printf("\nEnter 2 : display array");
printf("\nEnter 3 : insert element in to array");
printf("\nEnter 4 : delete element from array");
printf("\nEnter 5 : Exit");
printf("\n enter the choice \n");
scanf("%d", &ch);
switch (ch)
{
case 1:
create();
break;
case 2:
if (n == 0)
printf("Array is Empty\n");
else
display();
break;
case 3:
if (n == 0)
printf("Create the array first\n");
else
insert();
break;
case 4:
if (n == 0)
printf("Array is Empty\n");
else
del();
break;
case 5:
exit(0);
default:
printf("Invalid Choice\n");
}
}
}