-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSaumyaChitransh_DSLab.c
87 lines (83 loc) · 2.71 KB
/
SaumyaChitransh_DSLab.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
#include<stdio.h>
#include<conio.h>
#include<process.h>
int a[15], n, choice, b, num, max, temp, i, j;
void Display();
int main(){
clrscr();
printf("Enter size of array: ");
scanf("%d", &n);
printf("Enter elements of array: ");
for( i = 0; i < n; i++){
scanf("%d", &a[i]);
}
while(1){
printf("Enter your choice: \n1.Insert Element\n2.Insert at Middle\n3.Delete Element at index 2.\n4.Sorting in Decreased Order\n5.Display\n6.Exit\n");
scanf("%d", &choice);
switch(choice){
case 1: printf("Enter the index at which number is to be inserted: ");
scanf("%d",&b);
printf("Enter element to insert: ");
scanf("%d", &num);
for( j = n-1; j >= b; j--){
a[j+1] = a[j];
}
a[b] = num;
n = n + 1;
break;
case 2: printf("Enter the element to be inserted: ");
scanf("%d", &num);
if(n%2==0){
//inserting at n/2th position
b = n/2;
for( i = n-1; i>=b; i--){
a[i+1] = a[i];
}
a[b] = num;
n = n + 1;
}else{
//inserting at (n-1)/2th position
b = (n-1)/2;
for( i = n-1; i >= b; i--){
a[i+1] = a[i];
}
a[b] = num;
n = n+1;
}
break;
case 3: if(n>=3) {
for( i = 2; i < n; i++){
a[i] = a[i+1];
}
n = n - 1;
}else{
printf("Not sufficient elements for Deletion");
}
break;
case 4: for( i = 0; i < n - 1; i++){
max = i;
for( j = i+1; j<n; j++){
if(a[j] > a[max])
max = j;
}
temp = a[i];
a[i] = a[max];
a[max] = temp;
}
break;
case 5: Display(); break;
case 6: exit(0);
}
}
return 0;
getch();
}
void Display(){
if(n == 0){
printf("Array is empty!");
}else{
for(int i = 0; i < n; i++){
printf("a[%d] = %d\n", i, a[i]);
}
}
}