Results 1 to 8 of 8

Thread: [RESOLVED] how to get the median?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Resolved [RESOLVED] how to get the median?

    hello!
    how could you solve this?

    given: 1,3,5,6

    median = (3 + 5) / 2

    any idea?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to get the median?

    1. Get the number of values.
    2. Divide that number by 2.
    3. If the result of step 2 is a whole number then get the value at that index.
    4. If the result of step 2 is not a whole number then get the floor and ceiling of that number, get the two values at those indexes and average them.

    See the Math.Floor and Math.Ceiling functions.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: how to get the median?

    pls. give me an example i am new c#...

    you change ur pix huh!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to get the median?

    That's not C#. It's mathematics. Not only that, I'm guessing that this is homework, or at least a learning exercise. You've got the algorithm so give it a go. If you run into issues we can help, but you should try it yourself first.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: how to get the median?

    ok i understand...
    but i can't figure out how could i get the indexes
    imagine that i have 4 textboxes.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: how to get the median?

    Put the values from the textboxes into an array, sort it, and then implement the algo J provided using the resulting array indices.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: how to get the median?

    sorry for bothering here is the answer...

    int[] intArray = new int[] { Convert.ToInt16(txtEB1.Text), Convert.ToInt16(txtEB2.Text), Convert.ToInt16(txtEB3.Text) };
    Array.Sort(intArray);
    intMin = intArray[0];
    intMedian = intArray[1];
    intMax = intArray[2];

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] how to get the median?

    That's not the answer to the question you originally asked because that array only has three elements. You should write code that will work for any number of elements in the array, which you can do by following the algorithm that I provided.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width