Asked by:
How to work out the mode, median and range in C#?
Question

I'm starting out as a student and I need to work out the mode, median and range for 3 sets of numbers. If someone could help me learn how to find these averages in C# I would really appreciate it!
The numbers are: £1.50 £1.80 £3.65 £3.80 £4.20 £1.00 £3.65 £1.10 £1.35 £2.80 £1.30 £1.10 £5.60 £1.10 £3.70 £2.25 £4.00 £1.60 £2.00
Sunday, October 7, 2018 5:28 PM
All replies

The mode is the number that repeats more often in the list. If you want to do it in a "clever" way (instead of writing a loop that arranges them into a collection), you can use a linq query that groups by the value of the number, sorts by count descending and then takes the first entry.
The median is the number that sits in the middle of the list when it is sorted. You can do precisely that: sort the list (it can also be done with Linq, or you can use Array.Sort) and then take the number in the middle if there is an odd number of entries. If there is an even number (so none of the numbers is exactly in the middle), you return the average of the two numbers that are on both sides of the middle.
I'm not entirely sure about what you mean by "range". If you are referring to the difference between the largest and smallest number, then it is very easy once you have all the numbers sorted as required by the previous step: just subtract the first number from the last one.
Yes, this is a very "generic" answer and doesn't provide a code example. This is the standard practice in the forums when a student asks for help with an exercise. We can provide generic guidance, but not write the completed solution ready for copying and pasting  that would be cheating.
 Edited by Alberto PoblacionMVP Sunday, October 7, 2018 6:31 PM
 Proposed as answer by Wendy ZangMicrosoft contingent staff Tuesday, October 9, 2018 2:01 AM
Sunday, October 7, 2018 6:29 PM 
Hi chloepg,
Thank you for posting here.
For your question, if you want to work out the mode, here is a simple way with linq. Please check the code in the StackOverFlow for reference.
https://stackoverflow.com/questions/8260555/howtofindthemodeinarrayc
If you want to caculate median, you could try to use List.Median method. Please refer to the MSDN document with example.
https://msdn.microsoft.com/enus/querybi/m/listmedian
What is the rang in your description? If you want to generate a sequence of integral numbers within a specified range, you could refer to the MSDN document below.
If we misunderstand the mean of the range, please provide more details about it.
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com. Proposed as answer by Stanly Fan Tuesday, October 9, 2018 3:21 AM
Monday, October 8, 2018 6:26 AM