need help!!!! mean of even integers in queue,

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.

Anyone help it is due in 2days.
Can I see your code? It'll help alot. And wrap it in when you post it.
Hints:

i % 2 == 0 //Check if a number is even.

mean = (sum_of_integers)/(number_of_integers) //Neither number needs to be computed in one line.

Do these help?

-Albatross
Last edited on
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.

I don't know what deque and enque are but roughly here is what you need:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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;
}
to wolfgang
So, are you looking for non-even and then entering if stmt, because it says (!temp%2). Why is not used there?
Last edited on
Topic archived. No new replies allowed.