-1

I'm new to coding at all languages and as part of my course we are finding the median. Someone kindly helped me using constructors which my tutor has told me is quite a complex way to do it Is there an easier way than the code I attach.

The code also produces the wrong median so there is something wrong with the logic. I was told it should be:

(numberList.Count/2) and (numberList.Count/2 - 1), not -1 and -2?

Anyway the code below is what I have, please help me find the correct median in the most efficient way. Please also remember you're also speaking to someone with minimal understanding.

public class FindMedian
{
    public List<int> numbersList = new List<int>(); // list to store user input

    // constructor 
    public FindMedian(int n)
    {
        Console.WriteLine("Please Enter " + n + " numbers. \n\n");

        for (int i = 1; i < n + 1; i++)
        {
            Console.WriteLine("Number " + i + " : ");

            numbersList.Add(GetUserInput()); // call's GetUserInput
        }

        PrintData();
    }

    // returns int from Console. Will continue to run until a valid int is entered 
    private int GetUserInput() 
    {
        int temp = 0;
        bool numberValid = false;

        while (!numberValid)
        {
            try
            {
                temp = int.Parse(Console.ReadLine());
                numberValid = true;
            }

            catch
            {
                Console.WriteLine("Invalid entry. Please try again.\n");
                numberValid = false;
            }
        }

        return temp;
    }

    // prints data after user enteres the correct number of ints 
    private void PrintData()
    {
        // If list contains no data
        if (numbersList.Count < 3)
        {
            Console.WriteLine("No User Data Entered");
            return;
        }

        numbersList.Sort(); // Sorts list from smallest to largest

        int minimum = numbersList[0];
        int median;

        // if list count is even and median is average of two middle numbers
        if (numbersList.Count % 2 == 0)
        {
            median = (numbersList[(numbersList.Count / 2) - 2] + numbersList[(numbersList.Count / 2) - 1] / 2);
        }

        // if list count is odd and median is just middle number
        else 
        {
            median = numbersList[(numbersList.Count / 2)];
        }

        Console.WriteLine("Minimum Number : " + minimum);
        Console.WriteLine("Median Number : " + median);
    }
}
Tetsuya Yamamoto
  • 21,982
  • 5
  • 34
  • 53
Luke Heald
  • 45
  • 6
  • 2
    I'm pretty sure you need 0 and -1 instead. It looks like it's as simple as it gets. –  Oct 12 '17 at 07:26
  • There are plenty of working examples of median-finding already, including on Stack Overflow (see duplicates). Do keep in mind that the median value may or may not be part of the set of values, depending on whether you have an even or odd number of values, and depending on whether there are duplicate values or not. – Peter Duniho Oct 12 '17 at 07:55
  • Also keep in mind that in C# (and all other languages) operators have specific precedence and are not necessarily evaluated in lexical order. In particular, the `/` operator has higher precedence than the `+` operator, and so an expression containing both and no parens to override the precedence will do the `/` part first. – Peter Duniho Oct 12 '17 at 08:00
  • Thanks for your time Peter. Actually I read the majority of those posts already in the hope of understanding but as we haven't had any lectures yet on the theory behind this problem or even writing simple Syntax it has more been an exercise in finding the right answer. A frustrating way to learn but apparently a necessary one in computer science. The other posts were above what I was able to understand and wasn't able to make any sense of it. Hopefully in the coming months it will come together. Thanks again. – Luke Heald Oct 12 '17 at 11:15
  • The other 'duplicate' post for me doesn't answer how I might do that for user inputted data. This post may be useful for other beginners. – Luke Heald Oct 12 '17 at 11:18

1 Answers1

-1

@someone is correct it is simple as it gets. I believe the correct formula to put inside your PrintData() function is:

if (numbersList.Count % 2 == 0)
{
   median = (numbersList[(numbersList.Count / 2) - 1] + 
   numbersList[(numbersList.Count / 2)]) / 2;
}

The explanation is that in the case of an even numberList Count the / 2 operator gives you the second middle value in the list... hope this help.

  • This answer does not correct the more fundamental problem in the original code, in which the operator precedence is causing a divide-by-two to happen too early. If you want to see a _correct_ implementation of median, just visit the duplicates marked above. – Peter Duniho Oct 12 '17 at 07:58
  • @PeterDuniho you are right, sorry. It wasn't intentional... :) I edited my answer with the correct operator precedence. Thank you – Filippo Agostini Oct 12 '17 at 08:05