-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy patharray.c
More file actions
249 lines (231 loc) · 7.31 KB
/
array.c
File metadata and controls
249 lines (231 loc) · 7.31 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include<stdio.h>
// #define SENT 4
int arr[10];
int chk1 = 0;
int chk2 = 0;
// int size = sizeof(arr)/sizeof(int);
// int count_ele = 0;
// Choice 1: Function to traverse in array
void traverseArray(int arr[], int *count_ele){
printf("\n\t\t-------WELCOME TO ARRAY TRAVERSAL--------\n\n");
if((*count_ele) == 0){
printf("OOPS!!\n");
printf("Array is Empty\n\n");
return;
}
else{
printf("Elements Present in the array are: \n");
for (int i = 0; i < (*count_ele); i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}
}
// Choice 2: Function to insert element in array
void insetInArray(int arr[], int *count_ele,int size){
printf("\t\t-------WELCOME TO ELEMENT INSERTION in ARRAY--------\n\n");
// Checking Array Overflow Case
if((*count_ele)>size){
printf("Array Overflow\n");
return;
}
while((*count_ele)<=size){
printf("How many elements you want to insert in your array?\n");
int num;
scanf("%d",&num);
if(num == 0){
return;
}
chk1 = *count_ele;
(*count_ele) += num;
if((*count_ele)>size){
printf("Array Overflow\n");
(*count_ele) = chk1;
return;
}
else{
printf("Enter any '%d' integers in your array\n",num);
for(int i=chk2;i<(*count_ele);i++){
scanf("%d",&arr[i]);
}
chk2 = (*count_ele);
return;
}
}
}
// Choice 3: Delete from the array
void deleteFromArray(int arr[], int *count_ele){
printf("\t\t-------WELCOME TO ELEMENT DELETION in ARRAY--------\n\n");
// Checking Array Underflow Case
if((*count_ele) == 0){
printf("Array Underflow: Array is EMPTY\n");
return;
}
//Printing the array before deletion to make it easy for the user
printf("Elements Present in the array are: \n");
for (int i = 0; i < (*count_ele); i++){
printf("%d ",arr[i]);
}
//Code for deletion
printf("\n\nInsert the position you want to delete from the array\n");
int pos;
printf("Position: ");
scanf("%d",&pos);
if(pos>(*count_ele)){
printf("Invalid Position\nDeletion is not possible in the array\n");
return;
}
else{
int index = pos - 1;
(*count_ele) -= 1;
// use for loop to delete the element and update the index
for(int i = index; i < (*count_ele); i++) {
arr[i] = arr[i+1]; // assign arr[i+1] to arr[i]
}
printf ("\nThe resultant array is: \n");
for(int i=0;i<(*count_ele);i++){
printf("%d ",arr[i]);
}
printf("\n");
return;
}
}
// Choice 4: Linear Search in array
int LinearSearch(int arr[], int *count_ele){
printf("\t\t-------WELCOME TO LINEAR SEARCH in ARRAY--------\n\n");
if((*count_ele) == 0){
printf("Array is Empty. Cannot Search in it\n");
return -1;
}
else{
printf("Enter the element you are Searching for\n");
int key;
printf("Element: ");
scanf("%d",&key);
for(int i = 0;i<(*count_ele);i++){
if(arr[i] == key){
return i+1;
}
}
return -1;
}
}
//Choice 5: Selection Sorting the array
void SelectionSort(int arr[], int *count_ele){
printf("\t\t-------WELCOME TO SELECTION SORT in ARRAY--------\n\n");
if((*count_ele)==0){
printf("Array UnderFlow: Array is Empty");
return;
}
// Printing original array
printf("Array BEFORE Sorting\n");
for(int i=0;i<(*count_ele);i++){
printf("%d ",arr[i]);
}
printf("\n");
//Sorting the array
for(int i = 0;i<(*count_ele)-1;i++){
int minIndex = i;
for(int j = i+1;j<(*count_ele);j++){
if(arr[minIndex]>arr[j]){
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
//Printing the sorted array
printf("\nArray AFTER Sorting\n");
for(int i=0;i<(*count_ele);i++){
printf("%d ",arr[i]);
}
printf("\n");
return;
}
//Main includes on the menu code and choice calling
int main(){
int size = sizeof(arr)/sizeof(int);
int count_ele = 0;
// Option for whether to start in the menu or not
printf("\n\t\t\t\t****** WELCOME USER *******\n\n");
printf("Are you ready to deep-dive into the world of ARRAY\n");
printf("Do you want to play with ARRAY?\n");
printf("Enter 'y' to START else 'n' to EXIT\n");
char option;
printf("Your choice: ");
scanf("%s",&option);
//Loop to print menu again and again
while(option !='n'){
// MENU
printf("\n\t\t\t\t------- WELCOME TO THE MENU -------\n");
printf("\t1. Traverse in the array\n");
printf("\t2. Insert in the array\n");
printf("\t3. Delete in the array\n");
printf("\t4. Linear Search in the array\n");
printf("\t5. Selection Sort in the array\n");
printf("\t6. Exit\n\n");
int choice;
printf("Enter your choice: ");
scanf("%d",&choice);
printf("\n");
//Switch case code to call functions according to choice of user
switch (choice)
{
case 1:
if(choice == 1){
traverseArray(arr,&count_ele);
}
break;
case 2:
if(choice == 2){
insetInArray(arr,&count_ele,size);
}
break;
case 3:
if(choice == 3){
deleteFromArray(arr,&count_ele);
}
break;
case 4:
if(choice == 4){
int ans = LinearSearch(arr,&count_ele);
if(ans == -1){
printf("The given Element is NOT Present in the array\n");
}else{
printf("The given Element is Present in the array at position: %d \n",ans);
}
}
break;
case 5:
if(choice == 5){
SelectionSort(arr,&count_ele);
}
break;
case 6:
if(choice == 6){
printf("Are you sure you want to exit?\n");
printf("Enter 'y' to EXIT else 'n' to CONTINUE\n");
char temp;
scanf("%s",&temp);
if(temp == 'y'){
printf("\n\t\t\tWe are Sad you are leaving us so soon!!\n");
printf("\t\t\t\tGOODBYE!!\n");
printf("\t\t\tHOPE YOU HAD A GREAT TIME!!\n");
printf("\t\t\tPLEASE VISIT US AGAIN!!\n\n");
option = 'n';
}
else if(temp == 'n'){
option = 'y';
}
}
break;
default:
printf("\tInvalid Expression\n");
printf("\tInput a valid option from the menu\n\n");
break;
}
}
return 0;
}