forked from SaifulJnU/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrays in C
149 lines (141 loc) · 2.61 KB
/
Arrays in 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Array Operatons
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int a[100], n, i, value, position;
void create();
void display();
void insert();
void del();
void search();
void main()
{
int choice = 1;
clrscr();
create();
do
{
printf("\n\n*-------MENU-------*\n");
printf("1.DISPLAY\n");
printf("2.INSERT\n");
printf("3.DELETE\n");
printf("4.SEARCH\n");
printf("5.EXIT\n");
printf("------------------");
printf("\nEnter Your Choice:\t");
scanf("%d", &choice);
switch (choice)
{
case 1:
if(n == 0)
{
printf("No elements to display");
}else{
display();
}
break;
case 2:
insert();
display();
break;
case 3:
if(n == 0)
{
printf("No elements to delete");
}else{
del();
display();
}
break;
case 4:
if(n == 0)
{
printf("Array is empty.");
}else{
search();
}
break;
case 5:
exit(0);
break;
default:
printf("\nInvalid choice\n");
break;
}
}while (choice <= 5);
getch();
}
// Creating array elements
void create()
{
printf("\nEnter the size of array:\t");
scanf("%d", &n);
printf("\nEnter the elements of array:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
}
// displaying array elements
void display()
{
int i;
printf("\nArray elements are:\n");
for (i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
}
// Inserting element into an array
void insert()
{
printf("\nEnter the position for the new element:\t\n");
scanf("%d", &position);
if(position<=n)
{
printf("\nEnter the element to be inserted :\t");
scanf("%d", &value);
for (i = n - 1; i >= position - 1; i--)
{
a[i + 1] = a[i];
}
a[position - 1] = value;
n = n + 1;
}else
{
printf("Enter the correct position between 1 to %d\n",n);
}
}
// Deleting an array element
void del()
{
printf("\nEnter the position of the element to be deleted:\t");
scanf("%d", &position);
value = a[position - 1];
for (i = position - 1; i < n - 1; i++)
{
a[i] = a[i + 1];
}
n = n - 1;
printf("\nThe deleted element is = %d", value);
}
// Searching element in an array
void search()
{
int ele,count=0;
printf("\nEnter the element to search : \t\n");
scanf("%d", &ele);
for (i = 0; i <= n - 1; i++)
{
if (a[i] == ele)
{
printf("%d", i+1);
count++;
}
}
if(count != 1)
{
printf("\nElement not found in the Array.");
}
}