+ 1

Heaps

Can someone help me solve this problem? thank you so much A heap data structure can be efficiently implemented in a range using the C++ Standard Template Library (STL). In this exercise you are asked to utilize STL in order to build and manipulate a heap efficiently. In particular, you are requested to: 1. Make a heap consisting of 10 different integers of your own choosing. Include a statement to show the maximum element of the heap. 2. Add a new value that is the mean of the random values you creates in the previous step. Floor the value if needed (truncate decimal part). 3. Delete the maximum element of the heap and 4. Sort the heap. Write down the code to achieve all the operations mentioned above and clarify which section of the code does what.

12th Dec 2019, 2:45 AM
lakas
2 Respuestas
+ 2
have you try writing some code for this, if(true) show it; else try write some, users here don't write code for others but help em fix it
12th Dec 2019, 6:40 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
does this answers all the requirements ? #include<iostream> #include <vector> #include<algorithm> #include <stdlib.h> using namespace std; int main() { vector<int> v1; int i; // Initialized a vector for(i=0;i<10;i++){ v1.push_back(rand()); } // Converting vector into a heap using make_heap() make_heap(v1.begin(), v1.end()); // Displaying heap elements cout << "The heap elements Before Sorting are : "; for (int &val : v1) cout << val << " "; cout << endl; // sorting heap using sort_heap() sort_heap(v1.begin(), v1.end()); cout << "The maximum element of heap that are deleted : "; cout << v1.front() << endl; //using pop_heap() to delete maximum element pop_heap(v1.begin(), v1.end()); v1.pop_back(); // Displaying heap elements after sorting cout << "The heap elements after sorting are : "; for (int &val : v1) cout << val << " "; return 0; }
12th Dec 2019, 11:48 PM
lakas