-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy path31_Menu_for_Array.c
674 lines (630 loc) · 13.5 KB
/
31_Menu_for_Array.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#include<stdio.h>
#include<stdlib.h>
struct array
{
int *A;
int size;
int length;
};
void Display(struct array arr);
void Append(struct array *arr,int x);
void Insert(struct array *arr,int index,int x);
void Delete(struct array *arr,int index);
void swap(int*p,int*q);
void LinearSearch(struct array *arr,int key);
void LinearSearchSwapToPrev(struct array *arr,int key);
void LinearSearchSwapToHead(struct array *arr,int key);
void BinSearch(struct array *arr,int key);
void RevBinSearch(struct array arr,int low,int high,int key);
void Get(struct array arr,int index);
void Set(struct array *arr,int index,int x);
void Max(struct array arr);
void Min(struct array arr);
int Sum(struct array arr);
float Avg(struct array arr);
void Rev1(struct array *arr);
void Rev2(struct array *arr);
void Display(struct array arr);
void lShift(struct array *arr);
void rShift(struct array *arr);
void lRot(struct array *arr);
void rRot(struct array *arr);
void InsertSort(struct array *arr,int x);
void IsSort(struct array arr);
void Arrange(struct array *arr);
struct array* Merge(struct array *arr,struct array *arr2);
// All the functions are here->>>>
//Swap Function
void swap(int*p,int*q)
{
int temp;
temp=*p;
*p=*q;
*q=temp;
}
//Display Function
void Display(struct array arr)
{
int i;
printf("The elements in the array are: \n");
for(i=0;i<arr.length;i++)
{
printf("%d ",arr.A[i]);
}
printf("\n");
}
//Append Function
void Append(struct array *arr,int x)
{
int i;
if(arr->length<arr->size)
{
arr->A[arr->length]=x;
arr->length++;
}
else
{
printf("The array has no empty space to append");
}
}
//Insert Function
void Insert(struct array *arr,int index,int x)
{
int i;
if(arr->length<arr->size)
{
if(index>=0 && index<=arr->length)
{
for(i=arr->length;i>index;i--)
{
arr->A[i]=arr->A[i-1];
}
arr->A[index]=x;
arr->length++;
}
else
{
printf("You cannot insert the value due to the invalid index. \n");
}
}
else
{
printf("The size is not sufficient to insert the element in the array.\n");
}
}
//Delete Function
void Delete(struct array *arr,int index)
{
int i;
if(index>=0 && index<=arr->length)
{
for(i=index;i<arr->length;i++)
{
arr->A[i]=arr->A[i+1];
}
arr->length--;
}
else
{
printf("You can delete the element due to invalid \n");
}
}
//LinearSearch Normal Method Funtion
void LinearSearch(struct array *arr,int key)
{
int i,x=0;
for(i=0;i<arr->length;i++)
{
if(arr->A[i]==key)
{
printf("The element is found at location %d\n",i);
x=1;
break;
}
}
if(!x)
{
printf("The element is not present in the array\n");
}
}
//LinearSearch Move to previous index Function
void LinearSearchSwapToPrev(struct array *arr,int key)
{
int i,x=0;
for(i=0;i<arr->length;i++)
{
if(arr->A[i]==key)
{
swap(&arr->A[i],&arr->A[i-1]);
printf("The element is found at the location %d\n",i);
x=1;
break;
}
}
if(!x)
{
printf("The element is not present in the array\n");
}
}
//LinearSearch Move to Head/First index Function
void LinearSearchSwapToHead(struct array *arr,int key)
{
int i,x=0;
for(i=0;i<arr->length;i++)
{
if(arr->A[i]==key)
{
swap(&arr->A[i],&arr->A[0]);
printf("The element is found at the location %d\n",i);
x=1;
break;
}
}
if(!x)
{
printf("The element is not present in the array\n");
}
}
//Binary Search Normal method Function
void BinSearch(struct array *arr,int key)
{
int low=0;
int high=arr->length-1;
int mid,x=0;
while(low<=high)
{
mid=(low+high)/2;
if(arr->A[mid]==key)
{
printf("The element is found at index %d",mid);
x=1;
break;
}
else if(key<arr->A[mid])
{
high=mid-1;
}
else if(key>arr->A[mid])
{
low=mid+1;
}
}
if(x==0)
{
printf("The element is not found\n");
}
}
//Binary Search Recursion method Function
void RevBinSearch(struct array arr,int low,int high,int key)
{
int mid,x=0;
if(low<=high)
{
mid=(low+high)/2;
if(arr.A[mid]==key)
{
printf("The element is found at location %d\n",mid);
x=1;
}
else if(key<arr.A[mid])
{
RevBinSearch(arr,low,mid-1,key);
}
else if(key>arr.A[mid])
{
RevBinSearch(arr,mid+1,high,key);
}
}
else
{
printf("The element is not found\n");
}
}
//Get Function
void Get(struct array arr,int index)
{
if(index>=0 && index<arr.length)
{
printf("The number at given index is %d\n",arr.A[index]);
}
else
{
printf("The given index is not valid");
}
}
//Set Function
void Set(struct array *arr,int index,int x)
{
if(index>=0 && index<arr->length)
{
arr->A[index]=x;
}
else
{
printf("The given index is not valid");
}
}
//Max Function
void Max(struct array arr)
{
int i;
int max=arr.A[0];
for(i=0;i<arr.length;i++)
{
if(max<arr.A[i])
{
max=arr.A[i];
}
}
printf("\nThe maximum value in the array is: %d\n",max);
}
//Min Function
void Min(struct array arr)
{
int i,min=arr.A[0];
for(i=0;i<arr.length;i++)
{
if(arr.A[i]<min)
{
min=arr.A[i];
}
}
printf("The minimum value in the array is: %d\n",min);
}
//Sum Function
int Sum(struct array arr)
{
int sum=0,i;
for(i=0;i<arr.length;i++)
{
sum+=arr.A[i];
}
return sum;
}
//Average Function
float Avg(struct array arr)
{
return (float)Sum(arr)/arr.length;
}
//Reverse by copying the content to second array in reverse order and again copy to the initial array
void Rev1(struct array *arr)
{
int i,j;
int *B;
B=(int*)malloc(arr->size*sizeof(int));
for(i=arr->length-1,j=0;i>=0,j<arr->size;i--,j++)
{
B[j]=arr->A[i];
}
for(i=0;i<arr->size;i++)
{
arr->A[i]=B[i];
}
}
//Reverse the array bby swapping each equidistant element
void Rev2(struct array *arr)
{
int i,j;
for(i=0,j=arr->length-1;i<j;i++,j--)
{
swap(&arr->A[i],&arr->A[j]);
}
}
//Left Shift Function
void lShift(struct array *arr)
{
int i;
for(i=0;i<arr->length-1;i++)
{
arr->A[i]=arr->A[i+1];
}
printf("\nThe elements in the array after left shift are: \n");
for(i=0;i<arr->length-1;i++)
{
printf("%d ",arr->A[i]);
}
}
//Right Shift Function
void rShift(struct array *arr)
{
int i;
for(i=arr->length-1;i>0;i--)
{
arr->A[i]=arr->A[i-1];
}
printf("\nThe elements in the array after right shift are: \n");
for(i=1;i<=arr->length-1;i++)
{
printf("%d ",arr->A[i]);
}
}
//Left Rotation Function
void lRot(struct array *arr)
{
int i,x;
x=arr->A[0];
for(i=0;i<arr->length-1;i++)
{
arr->A[i]=arr->A[i+1];
}
arr->A[arr->length-1]=x;
printf("\nThe elements in the array after left rotation are: \n");
for(i=0;i<=arr->length-1;i++)
{
printf("%d ",arr->A[i]);
}
}
//Left Rotation Function
void rRot(struct array *arr)
{
int i,x;
x=arr->A[arr->length-1];
for(i=arr->length-1;i>0;i--)
{
arr->A[i]=arr->A[i-1];
}
arr->A[0]=x;
printf("\nThe elements in the array after right rotation are: \n");
for(i=0;i<=arr->length-1;i++)
{
printf("%d ",arr->A[i]);
}
}
//Insert in sorted array Funtion
void InsertSort(struct array *arr,int x)
{
int i;
if(arr->length<arr->size)
{
for(i=arr->length-1;x<arr->A[i];i--)
{
arr->A[i+1]=arr->A[i];
}
arr->A[i+1]=x;
arr->length++;
}
else
{
printf("The elements canot be inserted due to no empty spaces\n");
}
}
//Is sorted or not Function
void IsSort(struct array arr)
{
int i,x=1;
for(i=0;i<arr.length-1;i++)
{
if(arr.A[i]>arr.A[i+1])
{
x=0;
break;
}
}
if(x)
{
printf("The elements are in sorted order");
}
else
{
printf("The elements are not in sorted order");
}
}
//-ve on left side and +ve on right side
void Arrange(struct array *arr)
{
int i=0,j=arr->length-1;
while(arr->A[i]<0)
{
i++;
}
while(arr->A[j]>0)
{
j--;
}
if(i<j)
{
swap(&arr->A[i],&arr->A[j]);
}
}
struct array* Merge(struct array *arr,struct array *arr2)
{
int i,j,k;
i=j=k=0;
struct array *arr3=(struct array *)malloc(sizeof(struct array));
while(i<arr->length && j<arr2->length)
{
if(arr->A[i]<arr2->A[j])
{
arr3->A[k]=arr->A[i];
i++;k++;
}
else
{
arr3->A[k]=arr2->A[j];
j++;k++;
}
}
for(;i<arr->length;i++)
{
arr3->A[k]=arr->A[i];
k++;
}
for(;j<arr2->length;j++)
{
arr3->A[k]=arr2->A[j];
k++;
}
arr3->size=arr->size+arr2->size;
arr3->length=arr->length+arr2->length;
return arr3;
}
//Main Function
int main()
{
int i,ch,n,m,x,index;
struct array arr,arr2,*arr3;
printf("Enter the size of the array: \n");
scanf("%d",&arr.size);
arr.A=(int*)malloc(arr.size*sizeof(int));
arr.length=0;
printf("Enter the numner of element you want in the array: \n");
scanf("%d",&n);
arr.length=n;
printf("Enter the elements in the array: \n");
for(i=0;i<arr.length;i++)
{
scanf("%d",&arr.A[i]);
}
Display(arr);
do
{
//Menu Options
printf("\n\n************* M E N U *************\n");
printf("1. Display\n");
printf("2. Append\n");
printf("3. Insert\n");
printf("4. Delete\n");
printf("5. LinearSearch\n");
printf("6. LinearSearchSwapToPrev\n");
printf("7. LinearSearchSwapToHead\n");
printf("8. BinSearch\n");
printf("9. RevBinSearch\n");
printf("10. Get\n");
printf("11. Set\n");
printf("12. Max\n");
printf("13. Min\n");
printf("14. Sum\n");
printf("15. Average\n");
printf("16. Reverese using a new array\n");
printf("17. Reverse using swapping\n");
printf("18. Left Shifting\n");
printf("19. Right Shifting\n");
printf("20. Left Rotation\n");
printf("21. Right Rotation\n");
printf("22. Insert in Sorted Array\n");
printf("23. IsSorted or not\n");
printf("24. -ve on left side and +ve on right side\n");
printf("25. Merge the intial array with the new array, make sure all the array to be sorted\n");
printf("\n\nEnter your choice: \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
Display(arr);
break;
case 2:
printf("Enter the elements to be appended: \n");
scanf("%d",&x);
Append(&arr,x);
break;
case 3:
printf("Enter the index where the number has to be inserted: \n");
scanf("%d",&index);
printf("Enter the number to be inserted: \n");
scanf("%d",&x);
Insert(&arr,index,x);
break;
case 4:
printf("Enter the index where the number has to be deleted\n");
scanf("%d",&index);
Delete(&arr,index);
break;
case 5:
printf("Enter the number to be searched in the array\n");
scanf("%d",&x);
LinearSearch(&arr,x);
break;
case 6:
printf("Enter the number to be searched in the array\n");
scanf("%d",&x);
LinearSearchSwapToPrev(&arr,x);
break;
case 7:
printf("Enter the number to be searched in the array\n");
scanf("%d",&x);
LinearSearchSwapToHead(&arr,x);
break;
case 8:
printf("Enter the number to be searched in the array\n");
scanf("%d",&x);
BinSearch(&arr,x);
break;
case 9:
printf("Enter the number to be searched in the array\n");
scanf("%d",&x);
RevBinSearch(arr,0,arr.length-1,x);
break;
case 10:
printf("Enter the index whose value you want to know\n");
scanf("%d",&index);
Get(arr,index);
break;
case 11:
printf("Enter the index whose value you want to set\n");
scanf("%d",&index);
printf("Enter the value you want to set to the mentioned index\n");
scanf("%d",&x);
Set(&arr,index,x);
break;
case 12:
Max(arr);
break;
case 13:
Min(arr);
break;
case 14:
printf("The sum of the elements in the array is: %d\n",Sum(arr));
break;
case 15:
printf("The sum of the elements in the array is: %.2f\n",Avg(arr));
break;
case 16:
Rev1(&arr);
Display(arr);
break;
case 17:
Rev2(&arr);
Display(arr);
break;
case 18:
lShift(&arr);
break;
case 19:
rShift(&arr);
break;
case 20:
lRot(&arr);
break;
case 21:
rRot(&arr);
break;
case 22:
printf("Enter the number to be inserted: \n");
scanf("%d",&x);
InsertSort(&arr,x);
break;
case 23:
IsSort(arr);
break;
case 24:
Arrange(&arr);
break;
case 25:
printf("Enter the size of the 2nd array: \n");
scanf("%d",&arr2.size);
arr2.A=(int *)malloc(arr2.size*sizeof(int));
arr2.length=0;
printf("Enter the number of elements you want to enter: \n");
scanf("%d",&m);
arr2.length=m;
printf("Enter the elements in the array: \n");
for(i=0;i<arr2.length;i++)
{
scanf("%d",&arr2.A[i]);
}
arr3=Merge(&arr,&arr2);
Display(*arr3);
}
}
while(ch<26);
}