i need this practice problem in C++ part 1 28. This exercise uses a vector object to implement a priority queue class vpriority_queue. Assume that the name of the vector object included by composition is pqVector. The fol- lowing is an outline for the implementation of the class member functions: (i) Use the size() operation in the vector class to implement the priority-queue functions size() and empty(). (ii) The push() operation inserts a new element at the back of the vector by using push_back(). The elements in the vector are not ordered in any way. Assign the boolean data member recomputeMaxIndex the value true, since the new value may be the largest in the priority queue. Part (iii) describes this variable. pqVector pqVector before push(4) 3521 after push(4) 35214 (iii) The functions top() and pop() must compute the largest of the pqVector.size() el- ements. Do this computation with the private-member function findMaxIndex (). which assigns to the data member maxIndex the index of the maximum element. The functions top() and pop() both need the maximum value in the vec- lare a boolean data member recomputeMaxIndex, and initialize it to false. The functions top() and pop() use recomputeMaxIndex to avoid unnecessary calls to findMaxIndex(). (iv) The function top() returns the maximum value in pqVector. The function top() first checks to see whether the priority queue is empty. In that case, throw the exception underflowError. A second check looks at recomputeMaxIndex. If it is true, call findMaxIndex(), assign its return value to maxIndex, and set re- computeMaxIndex to false. In either case, top() returns the element pqVec- tor[maxIndex]. next steps are in the pictures then the code below is the last step. int findMaxIndex() const; // find the index of the maximum value in pqVector int maxIndex; // index of the maximum value bool recomputeMaxIndex; // do we need to compute the index of the maximum element?
i need this practice problem in C++
part 1
28. This exercise uses a
(i) Use the size() operation in the vector class to implement the priority-queue functions size() and empty().
(ii) The push() operation inserts a new element at the back of the vector by using push_back(). The elements in the vector are not ordered in any way. Assign the boolean data member recomputeMaxIndex the value true, since the new value may be the largest in the priority queue. Part (iii) describes this variable.
pqVector
pqVector
before push(4)
3521
after push(4) 35214
(iii) The functions top() and pop() must compute the largest of the pqVector.size() el- ements. Do this computation with the private-member function findMaxIndex (). which assigns to the data member maxIndex the index of the maximum element.
The functions top() and pop() both need the maximum value in the vec- lare a boolean data member recomputeMaxIndex, and initialize it to false. The functions top() and pop() use recomputeMaxIndex to avoid unnecessary calls to findMaxIndex().
(iv) The function top() returns the maximum value in pqVector. The function top() first checks to see whether the priority queue is empty. In that case, throw the exception underflowError. A second check looks at recomputeMaxIndex. If it is true, call findMaxIndex(), assign its return value to maxIndex, and set re- computeMaxIndex to false. In either case, top() returns the element pqVec- tor[maxIndex].
next steps are in the pictures then the code below is the last step.
int findMaxIndex() const;
// find the index of the maximum value in pqVector int maxIndex;
// index of the maximum value
bool recomputeMaxIndex;
// do we need to compute the index of the maximum element?
![cout << pq.top() << *
pq.pop();
CLASS vpriority_queue
Implement the class vpriority_queue, whose class declaration follows. Place the class in
the header file "vpqueue.h", and test the class by running Program 8-4.
template<typename T>
class vpriority_queue
{
public:
// output: 4
pq Vector
12 3
vpriority_queue ();
// default constructor. create an empty priority queue
const T& top() const;
// constant version.
Declaration
void push (const T& item);
// insert item into the priority queue.
// Postcondition: the priority queue has one more element
bool empty() const;
void pop();
// remove the highest priority (maximum) item from the
// priority queue.
// Precondition:
the priority queue is not empty. if it is
// empty, the function throws the underflowError exception
T& top();
// return the highest priority (maximum) item in the
// priority queue
// Precondition:
the priority queue is not empty. if it is
// empty, the function throws the under flowError exception
private:
"vpqueue.h"
// is the priority queue empty?
int size() const;
// return the size of the priority queue
vector<T> pqVector;
// vector that implements the priority queue.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fc4b4111f-0cb3-46a3-bff1-74a667fd5dd6%2F15aeb8bb-3d74-404c-9abb-bfd90ffb3f4a%2Fj7na874_processed.png&w=3840&q=75)
![(v) If recomputeMaxIndex is true, the function pop() finds the maximum element
by calling findMaxIndex(). The function must remove the highest priority item
from the vector, which vacates a position. Sliding the tail of the sequence toward
the front of the vector is inefficient. Implement the removal process by locating
the last element in the vector (pqVector.back()) and copying its value into the
vacated position. Remove the last element by using pqVector.pop_back(), and
set the value of recomputeMaxIndex to true, indicating that a subsequent call to
top() or pop() requires a new maximum.
The figures show the sequence of values in pqVector as push() and pop() operations ex-
ecute for the vpriority_queue object pq.
int arr [5] (6, 2, 3, 5, 3);
vpriority_queue<int> pq:
int i;
for (i=0;i< 5; i++)
pq.push(arr [i]);
cout << pq.top() << " ";
pq.pop();
6
cout << pq.top() << "
pq.pop();
pq.push (10);
pq.push (1);
4
cout << pq.top() << " ";
pq.pop();
4
pqVector
2 3 5 4
// output: 6
pq Vector
2 3 5
// output: 5
pqVector
4 2 3
pq Vector
4 2 3 10
pqVector
2 3 10 1
// output: 10.
pq Vector
4 2 3 1](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fc4b4111f-0cb3-46a3-bff1-74a667fd5dd6%2F15aeb8bb-3d74-404c-9abb-bfd90ffb3f4a%2F1yerno9_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 4 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)