Horje
c# odd even median Code Example
c# odd even median
public static void Main(string[] args)
        {
            int[] a = { 1, 4, 5, 8, 19, 12, 15 };

            List<int> l = new List<int>(a); l.Sort();
            List<int> even = new List<int>();   //hold even numbers
            List<int> odd = new List<int>();    //hold odd numbers
            int medianOdd;
            int medianEven;

            foreach (int num in l)
            {
                if (num % 2 == 0) even.Add(num);
                else odd.Add(num);
            }                                   //formula for median with odd vs even total length
            if (even.Count % 2 != 0) medianEven = even[even.Count / 2];
            else medianEven = (even[(even.Count - 1) / 2] + even[even.Count / 2]) / 2;

            if (odd.Count % 2 != 0) medianOdd = odd[odd.Count / 2];
            else medianOdd = (odd[(odd.Count - 1) / 2] + odd[odd.Count / 2]) / 2;

            Console.Write($"Even Median = {medianEven}  Odd Median = {medianOdd}");
            //median w/ Odd total length = length/2       vs Even length = (   (length/2 -1) + (length/2)  )  /2
        }




Csharp

Related
how to access individual characters in a string in c# Code Example how to access individual characters in a string in c# Code Example
remove character from string C# Code Example remove character from string C# Code Example
how to make an a i in unity Code Example how to make an a i in unity Code Example
built in methods to order a list c# Code Example built in methods to order a list c# Code Example
extension method c# Code Example extension method c# Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
10