sherlok
sherlok

Reputation: 11

Need help finding the middle value of 3 values given by a user

I'm going through a review my teacher gave the me for my first quarter final, and there is a question I don't remember the teacher going over. The question says "Get 3 values from the user, figure out and display the middle value", i.e. 10, 20, 30... 20 is the middle value. So far I have tried iterations of this code:

Console.WriteLine("Enter a value: ");
int value10 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter another value: ");
int value11 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter one last value: ");
int value12 = int.Parse(Console.ReadLine());

if (value10 > value11 && value11 > value12)
    Console.WriteLine("{0} is the middle value.", value11);

else if (value11 > value12 && value12 > value10)
    Console.WriteLine("{0} is the middle value.", value12);

else
    Console.WriteLine("{0} is the middle value.", value10);

No version I have written of this works.... WTF am I missing? I keep trying different combos of values, and directions of the lesser/greater than symbol. I've also tried different variations of if if else and else. Thanks in advance for any help.

Upvotes: 0

Views: 4259

Answers (4)

Andrew Palmer
Andrew Palmer

Reputation: 3013

It would probably be a bit cleaner to write the first part of your program to loop until you have filled a List<int> with as many values as you would like:

const int itemCount = 3;
List<int> inputValues = new List<int>(itemCount);

while(inputValues.Count < itemCount)
{
    Console.WriteLine("Enter a value: ");
    int parsed; 
    if(int.TryParse(Console.ReadLine(), out parsed))
        inputValues.Add(parsed);
    else
        Console.WriteLine("Please try again!");
}

The inputs naturally belong together as a collection that can be sorted, so storing your data this way, on the way in, is advantageous. (It is also a small step to use int.TryParse() in place of int.Parse(), if you prefer to handle invalid entries and continue taking inputs without throwing an Exception. The above code will just continue asking for values until it has three values it can parse successfully.)

A List can be sorted "in place" (without copying the list) with its .Sort() method, and the the list's indexer can be used to find the middle item.

int middleIndex = (itemCount - 1) / 2;  // hopefully, your list has an odd number of elements!
inputValues.Sort();
int middleValue = inputValues[middleIndex];

Console.WriteLine(middleValue);

Although it's not going to be the most efficient solution for your problem, particularly for larger numbers of elements, I thought I would still bring up that LINQ gives you a powerful syntax to order IEnumerable elements as well:

int middleValue = inputValues.OrderBy(x => x).ElementAt(middleIndex);

Upvotes: 0

Steve
Steve

Reputation: 9583

It the input is always three items and you just need the second one, why not put the inputs into a list, sort it then select the second value?

List<int> inputList = new List<int>(); 

inputList.Add(int.Parse(Console.ReadLine()));
Console.WriteLine("Enter another value: ");

inputList.Add(int.Parse(Console.ReadLine()));
Console.WriteLine("Enter one last value: ");

inputList.Addint.Parse(Console.ReadLine()));

inputList.Sort();
Console.WriteLine("{0} is the middle value.", inputList[1])

Upvotes: 1

Alexei Levenkov
Alexei Levenkov

Reputation: 100545

One option (that works for many elements too) is to sort and take middle element.

 var items = new[]{value10,value11, value12};
 Array.Sort(items);
 Console.Write(items[items.Length/2]);

Upvotes: 2

Lei Yang
Lei Yang

Reputation: 4335

Add 'OR' conditions to your if statement:

            if (value10 > value11 && value11 > value12 || value12 > value11 && value11 > value10)
            Console.WriteLine("{0} is the middle value.", value11);

        else if (value11 > value12 && value12 > value10 || value10 > value12 && value12 > value11)
            Console.WriteLine("{0} is the middle value.", value12);

Upvotes: 0

Related Questions