Write a function template that receives a priority queue and an output stream as parameters.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#include <iostream>
#include <iomanip>
#include <random>
#include <queue>
using namespace std;
template<typename T>
void distribution(const priority_queue<T>& pq, ostream& os);
int main()
{
priority_queue<int> pq1;
priority_queue<char> pq2;
priority_queue<double> pq3;
default_random_engine ran;
uniform_int_distribution<> dis1(0, 4), dis2(0,25), dis3(0,7);
int i;
cout << "Distribution of PQ1 with size() = " << pq1.size() << endl;
distribution(pq1, cout);
for (i = 0; i < 5; ++i)
{
pq1.push(dis1(ran));
cout << endl << "Distribution of PQ1 with size() = " << pq1.size()
<< endl;
distribution(pq1, cout);
}
for (i = 0; i < 10000; ++i)
{
pq1.push(dis1(ran));
pq2.push(static_cast<char>(dis2(ran) + 65));
pq3.push(dis3(ran) * 0.125);
}
cout << endl << "Distribution of PQ1" << endl;
distribution(pq1, cout);
cout << endl << "Distribution of PQ2" << endl;
distribution(pq2, cout);
cout << fixed << showpoint << setprecision(3);
cout << endl << "Distribution of PQ3" << endl;
distribution(pq3, cout);
return 0;
}
template<typename T>
void distribution(const priority_queue<T>& pq, ostream& os){
if(pq.empty()){
return;
}
//creating an array of T type to store all unique values in que
T values[pq.size()];
//creating an integer array to store counts of occurrences of
//values in que
int counts[pq.size()]={0};
//copying the priority queue contents to another queue
priority_queue<T> queCopy(pq);
//current number of unique values
int unique_values=0;
//looping until copied queue is empty
while(!queCopy.empty()){
//getting and removing top value from que
T value=queCopy.top();
queCopy.pop();
//flag to check if the current value exists in values array
bool found=false;
//looping and checking if value is there in values array
for(int j=0;j<unique_values;j++){
if(values[j]==value){
//found
found=true;
//incrementing count of current value
counts[j]++;
break; //exit inner loop
}
}
if(!found){
//not found,adding as new entry at index unique_values and incrementing
//the index
values[unique_values]=value;
counts[unique_values]=1;
unique_values++;
}
}
//displaying each unique values and its counts
for(int i=0;i<unique_values;i++){
os<<values[i]<<"("<<counts[i]<<")"<<endl;
}
}
/*OUTPUT*/
Distribution of PQ1 with size() = 0
Distribution of PQ1 with size() = 1
0(1)
Distribution of PQ1 with size() = 2
0(2)
Distribution of PQ1 with size() = 3
3(1)
0(2)
Distribution of PQ1 with size() = 4
3(1)
2(1)
0(2)
Distribution of PQ1 with size() = 5
3(1)
2(2)
0(2)
Distribution of PQ1
4(2043)
3(2032)
2(1978)
1(1979)
0(1973)
Distribution of PQ2
Z(355)
Y(368)
X(397)
W(390)
V(363)
U(380)
T(371)
S(401)
R(345)
Q(377)
P(411)
O(373)
N(386)
M(384)
L(397)
K(382)
J(418)
I(403)
H(387)
G(402)
F(425)
E(395)
D(367)
C(396)
B(353)
A(374)
Distribution of PQ3
0.875(1232)
0.750(1226)
0.625(1234)
0.500(1312)
0.375(1248)
0.250(1199)
0.125(1295)
0.000(1254)
Write a function template that receives a priority queue and an output stream as parameters. Lab 48 Due Date: See Blackboard Source File: /2336/48/1ab48.cpp Input: Output:under control of main functio...
This is my process, can you fix it for me? 0 Input: Output: Value: Under control of main function Under control of main function The purpose of this assignment is to become familiar with the process of providing overloaded operators for a class. The Rational class from Labs 02, 03, and 05 will be modified to provide overloaded operators for performing arithmetic on Rational numbers an overloaded unary minus for negating a Rational number (previously implemented as the additive an...