Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

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
        }
Comment

PREVIOUS NEXT
Code Example
Csharp :: unity change tmp text from script 
Csharp :: how to change textMesh Pro unity 
Csharp :: get waht is differnt between two arrays c# 
Csharp :: bootstrap modal 
Csharp :: The terminal process failed to launch: Path to shell executable "dotnet" is not a file or a symlink. 
Csharp :: how to convert from hexadecimal to binary in c# 
Csharp :: get name of project c# .net 
Csharp :: c# entity framework code first connection string 
Csharp :: merge point 
Csharp :: c# list sort by property string 
Csharp :: c# palidrone 
Csharp :: milliseconds to seconds c# 
Csharp :: c# LCP 
Csharp :: initialize ConsoleLoggerProvider in EF core 
Csharp :: WebClient c# with custom user agent 
Csharp :: unity position localposition 
Csharp :: c# read file current directory 
Csharp :: c# compile code at runtime 
Csharp :: c# 
Csharp :: .net Core Get File Request 
Csharp :: c# get set value 
Csharp :: c# declare an int list 
Csharp :: cannot convert string to generic type c# 
Csharp :: int value from enum in C# 
Csharp :: check if animation is playing unity 
Csharp :: unity show colliders 
Csharp :: c# print console 
Csharp :: linq where id in list 
Csharp :: how to use the mouse scroll wheel to move the camera in unity 
Csharp :: asp.net c# set session timeout 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =