-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.cpp
More file actions
101 lines (85 loc) · 2.25 KB
/
Copy pathheap.cpp
File metadata and controls
101 lines (85 loc) · 2.25 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
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
#include <iostream>
#include <math.h>
using namespace std;
#define max1 20
class stud {
public:
int marks[max1], total;
stud() {
for (int i = 0; i < max1; i++)
marks[i] = 0;
}
void createHeap();
void displayHeap();
void showmax();
void showmin();
};
void stud::createHeap() {
int i, j, par, temp, M;
cout << "\nEnter How many Students: ";
cin >> total;
for (i = 0; i < total; i++) {
cout << "\nEnter Marks: ";
cin >> marks[i];
M = marks[i];
j = i; // child
par = floor((j - 1) / 2);
while (marks[j] < marks[par] && j != 0) {
temp = marks[j];
marks[j] = marks[par];
marks[par] = temp;
j = par;
par = floor((j - 1) / 2);
}
cout << "\nCurrent Heap After Inserting: " << M << " is:\n";
displayHeap();
}
}
void stud::displayHeap() {
int i = 0, space = 6;
cout << endl;
while (i < total) {
if (i == 0 || i == 1 || i == 3 || i == 7 || i == 15) {
cout << endl << endl;
for (int j = 0; j < space; j++)
cout << " ";
space -= 2;
}
cout << " " << marks[i];
i++;
}
cout << endl;
}
void stud::showmin() {
cout << "\nMinimum Marks: " << marks[0] << endl;
}
void stud::showmax() {
int max = marks[0];
for (int i = 1; i < total; i++) {
if (max < marks[i])
max = marks[i];
}
cout << "\nMaximum Marks: " << max << endl;
}
int main() {
stud s1;
int ch, ans;
do {
cout << "\n1. Insert Marks";
cout << "\n2. Display Marks";
cout << "\n3. Show Max Marks";
cout << "\n4. Show Min Marks";
cout << "\n\nEnter Your Choice: ";
cin >> ch;
switch (ch) {
case 1: s1.createHeap(); break;
case 2: s1.displayHeap(); break;
case 3: s1.showmax(); break;
case 4: s1.showmin(); break;
default: cout << "\nInvalid Choice!";
}
cout << "\nDo you want to continue? (1 for yes): ";
cin >> ans;
} while (ans == 1);
return 0;
}