On 22 Mar, 17:55, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Oliver Sturm <oli...@sturmnet.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 "subcomponent"
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.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)));
}
}
}