Finding the Median in an Array

  • Thread starter Thread starter Bhadan
  • Start date Start date
B

Bhadan

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.
 
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
 
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
 
Oliver Sturm said:
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 "subcomponent"
array or he could mean each "top-level" jagged 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 "subcomponent"
array or he could mean each "top-level" jagged array.

Perhaps he'll let the group know...
 
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 "subcomponent"
array or he could mean each "top-level" jagged array.

Comically I had the same confusion but not until I'd written the
following. Figured I'd post anyway.

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

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

Console.ReadLine();
}
static double GetMedian(int[] pNumbers)
{
int size = pNumbers.Length;
int mid = size /2;
double median = (size % 2 != 0) ? (double)pNumbers[mid] :
((double)pNumbers[mid] + (double)pNumbers[mid-1]) / 2;
return median;
}
static double GetMedian(ArrayList pNumbers)
{
return GetMedian((int[])pNumbers.ToArray(typeof(int)));
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top