Thread: find the median

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    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. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> while (WOW >> aNumber && count << median)
    Fix the typo.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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
    Last edited by Dino; 03-30-2008 at 12:54 PM. Reason: typo
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    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. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    2.0 is a double. &#37; won't work with doubles. Try 2.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    count << median

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

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    Quote Originally Posted by Daved View Post
    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. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  3. ld.exe: cannot find -l-lstdc++
    By Tonto in forum Tech Board
    Replies: 3
    Last Post: 04-10-2007, 11:20 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM