Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to find the median of a list of integers in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

public class MedianOfIntArray_CSharp
{
    private static double FindMedianOfIntArray(List<int> list) {
        list.Sort();

        foreach (int num in list) {
            Console.Write(num);
            Console.Write(" ");
        }

        double median;
        int size = list.Count;

        if (size % 2 == 0) {
            median = (list[size / 2 - 1] + list[size / 2]) / 2.0;
        }
        else {
            median = list[size / 2];
        }

        return median;
    }

    public static void Main(string[] args)
    {
        List<int> list = new List<int> { 40, 70, 60, 55, 90, 45, 100, 80, 65, 50, 82, 58 };

        // List<int> list = new List<int>{ 24, 25, 26, 27, 28, 30, 32, 51, 34, 35, 36, 40, 60, 42, 49 };
        // 24 25 26 27 28 30 32 34 35 36 40 42 49 51 60
        // median = 34.00

        double median = FindMedianOfIntArray(list);

        Console.WriteLine("\nmedian = " + median);
    }
}



/*
run:
    
40 45 50 55 58 60 65 70 80 82 90 100 
median = 62.5
    
*/

 



answered Jul 18, 2024 by avibootz

Related questions

1 answer 98 views
1 answer 96 views
1 answer 48 views
2 answers 128 views
1 answer 93 views
93 views asked Mar 10, 2023 by avibootz
...