-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy path18 Menu Based problem.cpp
381 lines (348 loc) · 5.38 KB
/
18 Menu Based problem.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
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
// This is menu based project for some of the operations on array
// In this we are going to Display , Append , Insert , Delete , Linear & Binary Search ,grt ,set , max , min , sum , ave , reverse etc ;
#include <iostream>
using namespace std;
class Array
{
private:
int *A;
int size;
int length;
void swap(int *x,int *y);
public:
Array()
{
size=10;
length=0;
A=new int[size];
}
Array(int sz)
{
size=sz;
length=0;
A=new int[size];
}
~Array()
{
delete []A;
}
void Display();
void Append(int x);
void Insert(int index,int x);
int Delete(int index);
int LinearSearch(int key);
int BinarySearch(int key);
int Get(int index);
void Set(int index,int x);
int Max();
int Min();
int Sum();
float Avg();
void Reverse();
void Reverse2();
void InsertSort(int x);
int isSorted();
void Rearrange();
Array* Merge(Array arr2);
Array* Union(Array arr2);
Array* Diff(Array arr2);
Array* Inter(Array arr2);
};
void Array::Display()
{
int i;
cout<<"\nElements are\n";
for(i=0;i<length;i++)
cout<<A[i]<<" ";
}
void Array::Append(int x)
{
if(length<size)
A[length++]=x;
}
void Array::Insert(int index,int x)
{
int i;
if(index>=0 && index <=length)
{
for(i=length;i>index;i--)
A[i]=A[i-1];
A[index]=x;
length++;
}
}
int Array::Delete(int index)
{
int x=0;
int i;
if(index>=0 && index<length)
{
x=A[index];
for(i=index;i<length-1;i++)
A[i]=A[i+1];
length--;
return x;
}
return 0;
}
void Array::swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
int Array::LinearSearch(int key)
{
int i;
for(i=0;i<length;i++)
{
if(key==A[i])
{
swap(&A[i],&A[0]);
return i;
}
}
return -1;
}
int Array::BinarySearch(int key)
{
int l,mid,h;
l=0;
h=length-1;
while(l<=h)
{
mid=(l+h)/2;
if(key==A[mid])
return mid;
else if(key<A[mid])
h=mid-1;
else
l=mid+1;
}
return -1;
}
int Array::Get(int index)
{
if(index>=0 && index<length)
return A[index];
return -1;
}
void Array::Set(int index,int x)
{
if(index>=0 && index< length)
A[index]=x;
}
int Array::Max()
{
int max=A[0];
int i;
for(i=1;i<length;i++)
{
if(A[i]>max)
max=A[i];
}
return max;
}
int Array::Min()
{
int min=A[0];
int i;
for(i=1;i<length;i++)
{
if(A[i]<min)
min=A[i];
}
return min;
}
int Array::Sum()
{
int s=0;
int i;
for(i=0;i<length;i++)
s+=A[i];
return s;
}
float Array::Avg()
{
return (float)Sum()/length;
}
void Array::Reverse()
{
int *B;
int i,j;
for(i=length-1,j=0;i>=0;i--,j++)
B[j]=A[i];
for(i=0;i<length;i++)
A[i]=B[i];
}
void Array::Reverse2()
{
int i,j;
for(i=0,j= length-1;i<j;i++,j--)
{
swap(& A[i],& A[j]);
}
}
void Array::InsertSort(int x)
{
int i= length-1;
if( length== size)
return;
while(i>=0 && A[i]>x)
{
A[i+1]= A[i];
i--;
}
A[i+1]=x;
length++;
}
int Array::isSorted()
{
int i;
for(i=0;i<length-1;i++)
{
if(A[i]>A[i+1])
return 0;
}
return 1;
}
void Array::Rearrange()
{
int i,j;
i=0;
j= length-1;
while(i<j)
{
while( A[i]<0)i++;
while( A[j]>=0)j--;
if(i<j)swap(& A[i],& A[j]);
}
}
Array* Array::Merge(Array arr2)
{
int i,j,k;
i=j=k=0;
Array *arr3=new Array(length+arr2.length);
while(i<length && j<arr2.length)
{
if(A[i]<arr2.A[j])
arr3->A[k++]=A[i++];
else
arr3->A[k++]=arr2.A[j++];
}
for(;i<length;i++)
arr3->A[k++]=A[i];
for(;j<arr2.length;j++)
arr3->A[k++]=arr2.A[j];
arr3->length=length+arr2.length;
return arr3;
}
Array* Array::Union(Array arr2)
{
int i,j,k;
i=j=k=0;
Array *arr3=new Array(length+arr2.length);
while(i<length && j<arr2.length)
{
if(A[i]<arr2.A[j])
arr3->A[k++]=A[i++];
else if(arr2.A[j]<A[i])
arr3->A[k++]=arr2.A[j++];
else
{
arr3->A[k++]=A[i++];
j++;
}
}
for(;i<length;i++)
arr3->A[k++]=A[i];
for(;j<arr2.length;j++)
arr3->A[k++]=arr2.A[j];
arr3->length=k;
return arr3;
}
Array* Array::Inter(Array arr2)
{
int i,j,k;
i=j=k=0;
Array *arr3=new Array(length+arr2.length);
while(i<length && j<arr2.length)
{
if(A[i]<arr2.A[j])
i++;
else if(arr2.A[j]<A[i])
j++;
else if(A[i]==arr2.A[j])
{
arr3->A[k++]=A[i++];
j++;
}
}
arr3->length=k;
return arr3;
}
Array* Array::Diff(Array arr2)
{
int i,j,k;
i=j=k=0;
Array *arr3=new Array(length+arr2.length);
while(i<length && j<arr2.length)
{
if(A[i]<arr2.A[j])
arr3->A[k++]=A[i++];
else if(arr2.A[j]<A[i])
j++;
else
{
i++;
j++;
}
}
for(;i<length;i++)
arr3->A[k++]=A[i];
arr3->length=k;
return arr3;
}
int main()
{
Array *arr1;
int ch,sz;
int x,index;
cout<<"Enter Size of Array";
scanf("%d",&sz);
arr1=new Array(sz);
do
{
cout<<"\n\nMenu\n";
cout<<"1. Insert\n";
cout<<"2. Delete\n";
cout<<"3. Search\n";
cout<<"4. Sum\n";
cout<<"5. Display\n";
cout<<"6.Exit\n";
cout<<"enter you choice ";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter an element and index ";
cin>>x>>index;
arr1->Insert(index,x);
break;
case 2: cout<<"Enter index ";
cin>>index;
x=arr1->Delete(index);
cout<<"Deleted Element is"<<x;
break;
case 3:cout<<"Enter element to search";
cin>>x;
index=arr1->LinearSearch(x);
cout<<"Element index "<<index;
break;
case 4:cout<<"Sum is "<<arr1->Sum();
break;
case 5:arr1->Display();
}
}while(ch<6);
return 0;
}