-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAss10.cpp
More file actions
68 lines (66 loc) · 1.18 KB
/
Ass10.cpp
File metadata and controls
68 lines (66 loc) · 1.18 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
/*Read the marks obtained by students of second year in an online examination of particular subject. Find out maximum and minimum marks obtained in that subject. Use heap data structure. Analyze the algorithm.*/
#include<iostream>
using namespace std;
class Heap
{
int n;
int *minheap,*maxheap;
public:
void get();
void displayMin(){cout<<"Minimum marks are :"<<minheap[0]<<endl;}
void displayMax(){cout<<"Maximum marks are :"<<maxheap[0]<<endl;}
void upAdjust(bool,int);
};
void Heap::get()
{
cout<<"Enter number of students."<<endl;
cin>>n;
int k;
minheap=new int[n];
maxheap=new int[n];
cout<<"Enter marks of students."<<endl;
for(int i=0;i<n;i++)
{
cin>>k;
minheap[i]=k;
upAdjust(0,i);
maxheap[i]=k;
upAdjust(1,i);
}
}
void Heap::upAdjust(bool m,int l)
{
int s;
if(!m)
{
while(minheap[(l-1)/2]>minheap[l])
{
s=minheap[l];
minheap[l]=minheap[(l-1)/2];
minheap[(l-1)/2]=s;
l=(l-1)/2;
if(l==-1)
break;
}
}
else
{
while(maxheap[(l-1)/2]<maxheap[l])
{
s=maxheap[l];
maxheap[l]=maxheap[(l-1)/2];
maxheap[(l-1)/2]=s;
l=(l-1)/2;
if(l==-1)
break;
}
}
}
int main()
{
Heap H;
H.get();
H.displayMin();
H.displayMax();
return(0);
}