-2

So I'm struggling to get this piece to output a median value with 4 values. The output produces a value one above the actual middle value and I cannot seem to get it to output a decimal even when I change 2 to 2.0. I can get it to output a value with 3 numbers just haven't achieved it with 4.

    Console.Write("Median Value: ");
    var items = new[]{num1, num2, num3, num4 };
    Array.Sort(items);
    Console.WriteLine(items[items.Length/2]);

This work is an extension task in my computing class so I may have very well taken a completely wrong approach to this task. Thanks in advance

Ryan Easter
  • 60
  • 1
  • 12
  • 1
    Work a simple example with numbers. What is the median of the array `{1, 2}`? – Eric Lippert Oct 09 '17 at 23:35
  • var mediaValue = 0.0; var items = new[] { 1, 2, 3, 4,5 }; var itemsSum = items.Sum(); var getLengthItems = items.Length; if (getLengthItems % 2 == 0) { var firstValue = items[(items.Length / 2) - 1]; var secondValue = items[(items.Length / 2)]; mediaValue = (firstValue + secondValue) / 2.0; } if (getLengthItems % 2 == 1) { mediaValue = items[(items.Length / 2)]; } Console.WriteLine(mediaValue); Console.WriteLine("Enter to Exit!"); Console.ReadKey(); – Krishneil Oct 10 '17 at 04:13

2 Answers2

0
public decimal GetMedian(int[] array)
{
    int[] tempArray = array;
    int count = tempArray.Length;

    Array.Sort(tempArray);

    decimal medianValue = 0;

    if (count % 2 == 0)
    {
        // count is even, need to get the middle two elements, add them together, then divide by 2
        int middleElement1 = tempArray[(count / 2) - 1];
        int middleElement2 = tempArray[(count / 2)];
        medianValue = (middleElement1 + middleElement2) / 2;
    }
    else
    {
        // count is odd, simply get the middle element.
        medianValue = tempArray[(count / 2)];
    }

    return medianValue;
}

Your implementation works for odd sized collections. But when dealing with even sized collections, you find the middle pair of numbers, and then find the value that is half way (simple arithmetical average) between them. This is easily done by adding them together and dividing by two.

The method call based on your code:

Console.Write("Median Value: ");
int[] items = new int[] {num1, num2, num3, num4};
var median = GetMedian(items);
Console.WriteLine(median);

See it running on Ideone.

Vini Brasil
  • 4,828
  • 3
  • 22
  • 31
0

If you look at the explanations in Wikipedia, it's quite simple:

Easy explanation of the sample median

In individual series (if number of observation is very low) first one must arrange all the observations in order.

Then count(n) is the total number of observation in given data.

If n is odd then Median (M) = value of ((n + 1)/2)th item term.

If n is even then Median (M) = value of [(n/2)th item term + (n/2 + 1)th item term]/2.

How does that translate to code ?

using System;

namespace ConsoleApp1
{
    internal static class Program
    {
        private static void Main(string[] args)
        {
            var array = new[] {1, 2, 3, 4};

            Array.Sort(array);

            var n = array.Length;

            double median;

            var isOdd = n % 2 != 0;
            if (isOdd)
            {
                median = array[(n + 1) / 2 - 1];
            }
            else
            {
                median = (array[n / 2 - 1] + array[n / 2]) / 2.0d;
            }

            Console.WriteLine(median);
        }
    }
}

Note that you have to subtract one when getting the value of an element in the array since array indices are zero-based.

aybe
  • 12,940
  • 7
  • 49
  • 93