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
}
Also in C#:
- postasync c# returns null
- how to find object by ag unity
- Celsius to Fahrenheit c#
- c sharp tryparse
- JavaScriptSerializer() and convert to base64
- how to allow user import image c#
- C# events
- converting letter to ascii
- how to play sound in unity
- c# shuffle string array
- switch case c# range
- c# object to json string
- c# compare type
- how to redirect to extern page in .net core
- modal dismiss
- C# datareadeer return null
- for each property in object c#
- unity matchinfo
- c# function
- if and c#
- datagridview column color c#
- c# aspx return image
- read embedded resource c# xml
- unity fps counter