Hi, I need help with finding out mean of even integer data in a queue, someone help....
Need help for just finding mean of even integers,rest of it is done.
To hbjgd
Here's my code for mean of even integers data in a queue:
double meanque(queue &que)
{
queue temp;
int val;
double sum=0; mean=0;
int count=0;
while(!que.isEmptyqueue())
{
que.deque(val);
temp.enque(val);
if(val%2==0)
{
sum=sum+val;
count++;
}
}
mean=sum/count;
while(!temp.isEmptyqueue())
{
temp.deque(val);
que.enque(val);
}
return mean;
}
This's what I wrote but, didnot test it yet.
#include <iostream>
#include <queue>
double getMeanOfEvenValues(const std::queue<int> &p) // I don't think you need to pass by reference as I don't
// think we need to edit the original queue. If your queue implementation relies on being edited, you will need a second queue in the function.
{
std::queue<int> q(p.begin(), p.end());
double mean = 0;
int count = 0;
while (!q.empty())
{
int tmp = q.pop();
if (!(tmp % 2)) // If even
{
++count;
mean += tmp;
}
}
mean /= double(count);
return mean;
}