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?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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?

 

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.
Transcribed Image Text: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.
(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
Transcribed Image Text:(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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 4 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education