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?
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 4 images