1. ## find the median

Can anyone give me some kind of hint of what I am doing wrong here?
I am trying to find the median integer in the file. I have set it up the way my teacher asked us to, which was to make 2 passes through the file entitled "Section51.dat". The first pass (loop) is supposed to figure out how many numbers are in the file. This works correctly.
But the second loop is supposed to get the number in the middle of the file. For me it is just dividing the count in half which means I am obviously not doing something correct.
I also can't figure out how to do this in a case when you have an even set of integers in "Section51.dat" because then you will obviously need to average the two numbers in the middle of the file to get the median.
I have not had very much experience (or luck) with while loops yet, and I can't figure out how I am supposed to get the median integer. Can someone give me some guidance?
Code:
```#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream WOW;
WOW.open("Section51.dat");
int count = 0, aNumber;
while (WOW >> aNumber)
{
count++;
}
int median;
if (count / 2.0 == 0)
{
median = (count / 2.0) + ((count / 2.0) + 1) / 2.0;
}
else
{
median = count / 2.0;
}

while (WOW >> aNumber && count << median)
{
count++;
median--;
}
cout << aNumber;

return 0;
}```

2. >> while (WOW >> aNumber && count << median)
Fix the typo.

3. Code:
```	if (count / 2.0 == 0)
{
median = (count / 2.0) + ((count / 2.0) + 1) / 2.0;
}
else
{
median = count / 2.0;
}```
Let's take examples.

If count==4, then you want the mean between 2 & 3.
If count==5, you want 3.

The only number you can divide by 2 and get zero is zero. You want to look at the remainder of the divide by 2, therefore, use the modulo operator.

If the remainder is zero, for the case of count==4, the take the middle value (2) that's your first of two values.

If the remainder is not zero, then add 1 to count and take the middle value. So, if count==5, then add 1 to count (6) and divide by 2, to yield 3.

Todd

4. okay I changed the median code around, but now I have this code,and it is giving me an error on line 16.
I don't understand why I can't use '&#37;' here.
It says " invalid operands of types ‘int’ and ‘double’ to binary ‘operator%’ "
Code:
```#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream WOW;
WOW.open("Section51.dat");
int count = 0, aNumber;
while (WOW >> aNumber)
{
count++;
}
int median;
if (count % 2.0 == 0.0)
{
median = (count + 1) / 2;
}
else
{
median = count / 2.0;
}

while (WOW >> aNumber && count << median)
{
count++;
median--;
}
cout << aNumber;

return 0;
}```

5. 2.0 is a double. &#37; won't work with doubles. Try 2.

Todd

6. If the remainder is zero, for the case of count==4, the take the middle value (2) that's your first of two values.

If the remainder is not zero, then add 1 to count and take the middle value. So, if count==5, then add 1 to count (6) and divide by 2, to yield 3.
Also, there was a disconnect between what I said would work and what you implemented.

7. count << median

This returns a boolean? << is not a comparison operator is it, nor is median a stream.

8. It's a bitshift operator, so it does not cause a compile error even though it's wrong. It is also a typo that still needs to be fixed.

9. Originally Posted by Daved
It's a bitshift operator, so it does not cause a compile error even though it's wrong. It is also a typo that still needs to be fixed.
That's right! That's why I missed it. Dangerous! That's a creative way to screw up a program. Anyway I don't think the program will work even if it were a <. After all median <= count and usually < count prior to entering this loop.

10. You're right. There are other problems as well. Re-using the file stream like that won't work without extra code (there's a recent thread about this). JoeJoe, I would first try to get the code that determines the median position working, then worry about outputting the number at that position.