-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenuDrivenArrayImplementation.cpp
345 lines (315 loc) · 9.23 KB
/
menuDrivenArrayImplementation.cpp
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <bits/stdc++.h>
using namespace std;
const int maxArraySize = 10;
bool checkSpace(int size, int arr[])
{
if (size > maxArraySize)
return true;
return false;
}
void traverseArray(int iArray[], int arraySize)
{
for (int i = 0; i < arraySize; i++)
{
cout << iArray[i] << " ";
}
cout << endl;
}
void mainMenu()
{
cout << "Select What operation you want to perform." << endl;
cout << "\t 1.Traverse" << endl;
cout << "\t 2.Insertion" << endl;
cout << "\t 3.Deletion" << endl;
cout << "\t 4.Linear search" << endl;
cout << "\t 5.Binary search" << endl;
cout << "\t 6.Recursive binary search" << endl;
cout << "\t 7.Insertion Sort" << endl;
cout << "\t 10.Exit" << endl;
cout << "\n Enter your choice:";
}
void insertionMenu()
{
cout << "\t 1.Insertion at Beginning" << endl;
cout << "\t 2.Insertion at Specific Position" << endl;
cout << "\t 3.Insertion at End" << endl;
cout << "\t 0.Main Menu" << endl;
}
void deletionMenu()
{
cout << "\t 1.Deletion at Beginning" << endl;
cout << "\t 2.Deletion at Specific Position" << endl;
cout << "\t 3.Deletion at End" << endl;
cout << "\t 0.Main Menu" << endl;
}
void insertionSort(int inputArray[], int arraySize)
{
for (int i = 1; i < arraySize; i++)
{
int j = i;
while (j > 0 && inputArray[j - 1] > inputArray[j])
{
int temp = inputArray[j];
inputArray[j] = inputArray[j - 1];
inputArray[j - 1] = temp;
j--;
}
}
}
void insertionSubMenu(int inputArray[], int &arraySize, int userInput)
{
switch (userInput)
{
int insertingInt;
case 0:
cout << "Going back to main menu." << endl;
break;
case 1: // insertion at start
cout << "Enter the integer to you want to insert at start:";
cin >> insertingInt;
cout << endl;
arraySize++;
if (checkSpace(arraySize, inputArray))
{
cout << "Overflow, array is already full" << endl;
break;
};
for (int i = arraySize; i > 0; i--)
{
inputArray[i] = inputArray[i - 1];
}
inputArray[0] = insertingInt;
cout << "New Array is: ";
traverseArray(inputArray, arraySize);
break;
case 2: // inserting at mid position
int position;
cout << "Enter the position to you want to insert at and then integer:";
cin >> position >> insertingInt;
arraySize++;
if (checkSpace(arraySize, inputArray))
{
arraySize--;
cout << "Overflow, array is already full" << endl;
break;
};
if (position > arraySize || position > maxArraySize)
{
cout << "Invalid Position going back to main menu" << endl;
arraySize--;
break;
}
if (position == arraySize)
{
inputArray[position - 1] = insertingInt;
cout << "New Array is: ";
traverseArray(inputArray, arraySize);
break;
}
for (int i = arraySize; i >= position; i--)
{
inputArray[i] = inputArray[i - 1];
}
inputArray[position - 1] = insertingInt;
cout << "New Array is: ";
traverseArray(inputArray, arraySize);
break;
case 3: // insertion at end
arraySize++;
if (checkSpace(arraySize, inputArray))
{
cout << "Sorry Array is full" << endl;
break;
};
cout << "Enter the integer to you want to insert at end:";
cin >> insertingInt;
cout << endl;
inputArray[arraySize - 1] = insertingInt;
cout << "New Array is: ";
traverseArray(inputArray, arraySize);
break;
default:
cout << "Invalid choice going back to main menu";
break;
}
}
void deletionSubMenu(int inputArray[], int &arraySize, int userInput)
{
switch (userInput)
{
int removingPosition;
case 0:
cout << "Going back to main menu." << endl;
break;
case 1: // deletion at start
for (int i = 0; i < arraySize - 1; i++)
{
inputArray[i] = inputArray[i + 1];
}
cout << "New Array is: ";
arraySize--;
traverseArray(inputArray, arraySize);
break;
case 2:
cout << "Enter the positon where you want to delete the number:";
cin >> removingPosition;
if (removingPosition > arraySize)
{
cout << "Invalid Position going back to main menu" << endl;
break;
}
for (int i = removingPosition - 1; i < arraySize - 1; i++)
{
inputArray[i] = inputArray[i + 1];
}
cout << "New Array is: ";
arraySize--;
traverseArray(inputArray, arraySize);
break;
case 3: // deletion at end
arraySize--;
cout << "New Array is: ";
traverseArray(inputArray, arraySize);
break;
default:
cout << "Invalid choice going back to main menu";
break;
}
}
int linearSearch(int iArray[], int sizeOfArray, int key)
{
for (int i = 0; i < sizeOfArray; i++)
{
if (iArray[i] == key)
return i;
}
return -1;
}
int binarySearch(int inputArray[], int &arraySize, int key)
{
cout << "If your array is not sorted then element position will be wrong in array" << endl;
insertionSort(inputArray, arraySize);
int start = 0;
int end = arraySize - 1;
while (start <= end)
{
int mid;
mid = start + (end - start) / 2;
if (inputArray[mid] == key)
{
return mid;
}
if (inputArray[mid] < key)
{
start = mid + 1;
}
if (inputArray[mid] > key)
{
end = mid - 1;
}
}
return -1;
}
int recursiveBinarySearch(int array[], int start, int end, int key)
{
cout << "If your array is not sorted then element position will be wrong in array" << endl;
if (start <= end)
{
int mid = start + (end - start) / 2;
if (array[mid] == key)
return mid;
if (array[mid] < key)
return recursiveBinarySearch(array, mid + 1, end, key);
if (array[mid] > key)
return recursiveBinarySearch(array, start, mid - 1, key);
}
return -1;
}
int main()
{
cout << "Please enter number of element you want to add." << endl;
int arraySize = 0;
cin >> arraySize;
cout << endl;
cout << "Please enter " << arraySize << " integer digit to view all opertations which can be performed." << endl;
int inputArray[maxArraySize];
// getting array values
for (int i = 0; i < arraySize; i++)
{
cin >> inputArray[i];
cout << endl;
}
int userInput = 0;
while (userInput != 10)
{
mainMenu();
cin >> userInput;
cout << endl;
switch (userInput)
{
case 1:
{ // traverse the array
traverseArray(inputArray, arraySize);
break;
}
case 2:
{
insertionMenu();
cin >> userInput;
cout << endl;
insertionSubMenu(inputArray, arraySize, userInput);
break;
}
case 3:
{
deletionMenu();
cin >> userInput;
cout << endl;
deletionSubMenu(inputArray, arraySize, userInput);
break;
}
case 4:
{
int searchingElement;
cout << "Enter Number you want to search:";
cin >> searchingElement;
cout << endl;
int linearsearchResult = linearSearch(inputArray, arraySize, searchingElement);
linearsearchResult == -1 ? cout << "Element not found" << endl : cout << "Element found at " << linearsearchResult + 1 << endl;
break;
}
case 5:
{
int searchingElement;
cout << "Enter Number you want to search:";
cin >> searchingElement;
cout << endl;
int binarySearchResult = binarySearch(inputArray, arraySize, searchingElement);
binarySearchResult == -1 ? cout << "Element not found" << endl : cout << "Element found at " << binarySearchResult + 1 << endl;
break;
}
case 6:
{
int searchingElement;
cout << "Enter Number you want to search:";
cin >> searchingElement;
cout << endl;
insertionSort(inputArray, arraySize); // sorting before sending array for binary search
int recursiveBinarySearchResult = recursiveBinarySearch(inputArray, 0, arraySize - 1, searchingElement);
recursiveBinarySearchResult == -1 ? cout << "Element not found" << endl : cout << "Element found at " << recursiveBinarySearchResult + 1 << endl;
break;
}
case 7:
insertionSort(inputArray, arraySize);
traverseArray(inputArray, arraySize);
break;
case 10:
cout << "Exiting program";
break;
default:
cout << "Wrong input please choose another input" << endl;
break;
}
}
return 0;
}