Finding the Median in an Array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bhadan

    Finding the Median in an Array

    Hello,

    I have several jagged arrays which have been sorted.
    I'm trying to find the median of each array. Any tips appreciated.

    TIA.
    Bhads.



  • Jon Skeet [C# MVP]

    #2
    Re: Finding the Median in an Array

    On Mar 22, 3:56 pm, "Bhadan" <m...@here.comw rote:
    I have several jagged arrays which have been sorted.
    I'm trying to find the median of each array. Any tips appreciated.
    Well, which bit are you stuck on? Completely stuck for ideas, or just
    unsure of implementation?

    Are you trying to find the median of each part of each jagged array,
    or each jagged array "as a whole"?

    I'd start by thinking of how you'd do it for a "normal" array...

    Jon

    Comment

    • Oliver Sturm

      #3
      Re: Finding the Median in an Array

      Hello Bhadan,
      >I have several jagged arrays which have been sorted.
      >I'm trying to find the median of each array. Any tips appreciated.
      Hm... are you asking how to calculate a median over a list of values? Look
      here: http://www.statcan.ca/english/edu/po...ian/median.htm


      Oliver Sturm
      --

      Comment

      • Mark Rae

        #4
        Re: Finding the Median in an Array

        "Oliver Sturm" <oliver@sturmne t.orgwrote in message
        news:xn0f40koy3 4n3fq00f@msnews .microsoft.com. ..
        >>I have several jagged arrays which have been sorted.
        >>I'm trying to find the median of each array. Any tips appreciated.
        >
        Hm... are you asking how to calculate a median over a list of values?
        I sort of assumed he wasn't, since he mentioned jagged arrays, or "arrays of
        arrays" if you prefer...



        Comment

        • Oliver Sturm

          #5
          Re: Finding the Median in an Array

          Hello Mark,
          >I sort of assumed he wasn't, since he mentioned jagged arrays, or "arrays
          >of arrays" if you prefer...
          >http://msdn2.microsoft.com/en-us/library/2s05feca.aspx
          I had the same thoughts, but then he says rather clearly "I'm trying to
          find the median of each array".

          If we're talking several arrays, I don't think there's a clear-cut
          algorithm anyway - he'd just have to calculate a value per array and take
          it from there. Then what value would that be? Another median maybe? Or a
          mean? Depends, I guess...


          Oliver Sturm
          --

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Finding the Median in an Array

            Oliver Sturm <oliver@sturmne t.orgwrote:
            I sort of assumed he wasn't, since he mentioned jagged arrays, or "arrays
            of arrays" if you prefer...
            http://msdn2.microsoft.com/en-us/library/2s05feca.aspx
            >
            I had the same thoughts, but then he says rather clearly "I'm trying to
            find the median of each array".
            Unfortunately that's not terribly clear, as he has "several jagged
            arrays". If he had a single jagged array, then the meaning of "each
            array" would be clear. As it is, he could mean each "subcompone nt"
            array or he could mean each "top-level" jagged array.

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Mark Rae

              #7
              Re: Finding the Median in an Array

              "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
              news:MPG.206cd3 51ea6c051498d9f 9@msnews.micros oft.com...
              Unfortunately that's not terribly clear, as he has "several jagged
              arrays". If he had a single jagged array, then the meaning of "each
              array" would be clear. As it is, he could mean each "subcompone nt"
              array or he could mean each "top-level" jagged array.
              Perhaps he'll let the group know...


              Comment

              • DeveloperX

                #8
                Re: Finding the Median in an Array

                On 22 Mar, 17:55, Jon Skeet [C# MVP] <s...@pobox.com wrote:
                Oliver Sturm <oli...@sturmne t.orgwrote:
                >I sort of assumed he wasn't, since he mentioned jagged arrays, or "arrays
                >of arrays" if you prefer...
                >http://msdn2.microsoft.com/en-us/library/2s05feca.aspx
                >
                I had the same thoughts, but then he says rather clearly "I'm trying to
                find the median of each array".
                >
                Unfortunately that's not terribly clear, as he has "several jagged
                arrays". If he had a single jagged array, then the meaning of "each
                array" would be clear. As it is, he could mean each "subcompone nt"
                array or he could mean each "top-level" jagged array.
                >
                --
                Jon Skeet - <s...@pobox.com >http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too
                Comically I had the same confusion but not until I'd written the
                following. Figured I'd post anyway.

                using System;
                using System.Collecti ons;
                namespace ConsoleApplicat ion3
                {
                class Class1
                {
                [STAThread]
                static void Main(string[] args)
                {
                Console.WriteLi ne("Median = {0}",GetMedian( new int[]
                {1,2,4,7,9,11}) ); //5.5
                Console.WriteLi ne("Median = {0}",GetMedian( new int[]
                {1,2,10,9,11})) ; //10
                Console.WriteLi ne("Median = {0}",GetMedian( new int[]
                {1,8,10,9,11,15 ,15})); //9
                Console.WriteLi ne("Median = {0}",GetMedian( new int[]
                {1,4,10,12,33,8 8})); //11

                Console.WriteLi ne("Median = {0}",GetMedian( new ArrayList(new int[]
                {1,2,3,100}))); //2.5

                Console.ReadLin e();
                }
                static double GetMedian(int[] pNumbers)
                {
                int size = pNumbers.Length ;
                int mid = size /2;
                double median = (size % 2 != 0) ? (double)pNumber s[mid] :
                ((double)pNumbe rs[mid] + (double)pNumber s[mid-1]) / 2;
                return median;
                }
                static double GetMedian(Array List pNumbers)
                {
                return GetMedian((int[])pNumbers.ToArr ay(typeof(int)) );
                }
                }
                }

                Comment

                Working...