Simple Median hekp

This is a discussion on Simple Median hekp within the C++ Programming forums, part of the General Programming Boards category; I'm trying to find the median in an array and am stuck at the end of my code, obviously grade[median] ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    Simple Median hekp

    I'm trying to find the median in an array and am stuck at the end of my code, obviously grade[median] isn't showing me the grade in the median location, like i'm trying to do. of course, it gives me an error. So what would be the proper way to make it show me the median location in the array? Also, this will only work correct with an odd number of array elements and I know to find the median in an array of even amount of elements, you take the 2 middle terms then divide by 2. Any help on getting the location for the 2 middle terms and dividing by 2?

    ps- I know it needs to be sorted first but that is being taken care of by a different person. My job is to come up with the algorithm for finding the median.

    Code:
    int grade[25];
    int i = 0;
    float median;
    
    
    
    do
    {
    
    cout << "Enter grades, to quit enter 999\n";
    cin >> grade[i];
    i++;
    }
    while(grade[i-1] != 999);
    
    i = i-1; 
    
    
    
    for (int j=0; j<i; j++)
    {
    cout << grade[j] << endl;
    }
    
    median = (i+1) / 2;
    
    cout << grade[median] << endl;

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    If the sorted sequence has an odd length, take the middle score as the median (length/2). If the length is even, you have to divide the sequence into two parts of equal length, and take the average of the last score in part A and the first score in part B to get the median.
    Last edited by whiteflags; 10-16-2010 at 10:45 AM.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by whiteflags View Post
    If the sorted sequence has an odd length, take the middle score as the median (length/2). If the length is even, you have to divide the sequence into two parts of equal length, and take the average of the last score in part A and the first score in part B to get the median.
    Thank you, but do you know how to display the score in the [median] spot of the array?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    Quote Originally Posted by nick753
    Thank you, but do you know how to display the score in the [median] spot of the array?
    Once you have found it, you would display it like how you would display any other score.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    33
    You need to improve your keyboard input routines. Iíve tidied it up slightly but if you enter a character then your program will crash.

    If you reference your grade array with an index > 24 then it will crash, (0 Ė 24 allowed).

    Donít forget that with integer types, a value of 11 divided by 2 = 5 and not the expected 5.5.

    Good luck.


    Code:
    int grade[25];
    int i = 0;
    float median;
    
    int x; ///////////////////////
    
    do
    {
    
    cout << "Enter grades, to quit enter 999\n";
    /////////////////////////////////////////////////////////////cin >> grade[i];
        cin >> x; /////////
        if (x != 999)     ////////////
        {                 //////////
            grade[i] = x;  ////////////////
            i++;           /////////
        }                 /////////
    
    }
    ////////////////////////////////////////////////////////////while(grade[i-1] != 999);
    while ((i < 25) && (x != 999)); //////////////
    
    ////////////////////////////////////////////////////////////i = i-1;
    
    
    for (int j=0; j<i; j++)
    {
    cout << grade[j] << endl;
    }
    
    ///////////////////////////////////////////////////////////////median = (i+1) / 2;
    
    //?????????????????????????????????????????????????????????????//cout << grade[median] << endl;
    
    
        // to get the 2 required indexes for an even number sequence
        // i = number of integers input
        // a = i / 2
        // b = a + 1
        // median = ((grade[a - 1] + grade[b - 1}) / 2  // -1 because array starts at 0, not 1
        // substitute a and b into:
        // median = (grade[(i / 2) - 1] + grade[((i / 2) + 1) - 1]) / 2
        // median = (grade[(i / 2) - 1] + grade[i / 2]) / 2;
    
        if (i % 2 == 0) // even number of integers input
        {
    
        }
        else // odd
        {
    
        }
    
    
        cout << "\Median = " << median << endl;

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    What is with all the slash nonsense.

    If you want to make improvements we might as well use vectors so that proper indexing is almost no work at all.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    EDIT: What I posted before was not helpful. This might be moreso.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	int grade[25];
    	int i = 0;
    	float median;
    
    	do
    	{
    		cout << "Enter grades, to quit enter 999\n";
    		cin >> grade[i];
    		i++;
    	}
            // What if they enter more than 25 grades to be meanies?
    	while(grade[i-1] != 999 && i < 25);
    
    	i = i-1; 
    
           // Sort at some point. I just entered numbers in numerical order because I am nice.
    
    	for (int j=0; j<i; j++)
    	{
    		cout << grade[j] << endl;
    	}
    
    	if(i % 2 == 1)
    	{
    		median = grade[ (int)floor(i / 2) ];		
    	}
    	else
    	{
    		median = 0.5f * ( grade[ (i-1) / 2 ] + grade[ i / 2 ] );
    	}
    
    	// Erg, median is declared as float. Try not to index an array with a float type
    	//cout << grade[median] << endl;
    	cout << median << endl;
    }
    Last edited by QuadraticFighte; 10-17-2010 at 02:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21