5

I need a user to input three numbers and then i need to make the program write out the largest to smallest numbers.

example: USER inputs 16, 3 and 45 output would be: Largest number is: 45, middle number is: 16 and smallest number is: 3;

at the moment i have the numbers stored in different variables and use if, else if statements to get the biggest and smallest, but I dont know how to get the middle number out with else if.

if (number1 <= number2
    && number1 >= number3 & number1 <= number3
    && number1 >= number2)
{
    middle = number1;
}
else if (number2 <= number1
    && number2 >= number3 & number2 <= number3
    && number2 >= number1)
{
    middle = number2;
}
else if (number3 <= number1
    && number3 >= number2 & number3 <= number2
    && number3 >= number1)
{
    middle = number3;
}

Is it even possible?

Do I need to make the user input the values into an Array which I then need to sort and then write out the sorted array?

EDIT:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practise
{
    class Practise
    {
        static void Main(string[] args)
        {

            int[] newArray = new int[3];
            for (int i = 0; i < newArray.Length; i++)
            {
                newArray[i] = Convert.ToInt32(Console.ReadLine());
            }
            Array.Sort(newArray);
            int lowestNumber = newArray[0];
            int middle = newArray[1];
            int highest = newArray[2];

            Console.WriteLine("How can i only type out variables?" + highest + How can i only type out variables?" + middle + "How can i only type out variables?" + lowestNumber);
            Console.ReadLine();

        }
    }
}

The Array sort worked, used Selman22's solution! What do I need to type in the Console.WriteLine(); only to write out variables without the values being added.

> example:  highest = 15  middle = 10 lowestNumber = 5
> Console.WriteLine(highest + middle + lowestNumber); would do 15 + 10
> + 5 = 30. If i put "text" in between they obviously does not add up but what do I type to only get values out?

I am really thankful for all answers, this site is really helpful!

EDIT:

Console.WriteLine(" " + highest + " " + middle + " " + lowestNumber);

Is there any other way of writing out the values of the variables than by adding " " in between? If i do

Console.Writeline(highest, middle ,lowestNumber);

If i put that it says an error like cant convert highest into string etc..

user4032450
  • 61
  • 1
  • 4
  • 7
    +1 how many newbies even show us the code they tried? – John Saunders Sep 11 '14 at 20:17
  • There is a function that sort arrays automatically if I remember well. It would be easier to use an array yes. – P1kachu Sep 11 '14 at 20:19
  • 2
    @JohnSaunders, funny thing is none of the answers attempted to fix the OP's code, but provide new code from scratch (other than the variable names). – gunr2171 Sep 11 '14 at 20:23
  • @gunr2171 Eh? OP specifically asked about a solution using an array and sorting. – tnw Sep 11 '14 at 20:24
  • @tnw, true, but then the code in the question is irrelevant, because it uses if statements and no arrays. – gunr2171 Sep 11 '14 at 20:25
  • Another idea (probably not a great one, but worth thinking about): You could also use an approach calling Math.Min and Math.Max repeatedly on the three inputs. – phoog Sep 11 '14 at 20:26
  • `int middle = Math.Min(Math.Min(Math.Max(number1, number2), Math.Max(number2, number3)), Math.Max(number1, number3));` – Habib Sep 11 '14 at 20:41
  • Take a look at [this question](http://stackoverflow.com/questions/1582356/fastest-way-of-finding-the-middle-value-of-a-triple), although it is in Java, but you can replicate the logic in C#, also instead of array index use your variables. – Habib Sep 11 '14 at 21:00
  • 1
    Acc. to your last question(_EDIT_), you can use `Console.WriteLine(" {0} {1} {2}", highest, middle, lowestNumber);`. But in general you should ask new questions instead. – Tim Schmelter Sep 11 '14 at 21:03
  • Thanks exactly what i was looking for! What I dont get is why it thinks I want to convert it to string as i already defined it as an int. I mean when i put Console.WriteLine(highest, middle, lowestNumber); @Tim Schmelter – user4032450 Sep 11 '14 at 21:06
  • @user4032450: because[`Console.WriteLine`](http://msdn.microsoft.com/en-us/library/828t9b9h(v=vs.110).aspx) wants a string(you want to write text). But you have passed some integers. The first parameter must be a string, the other parameters can be a different type(as in my example above), then `ToString()` is used to convert that to `string`. – Tim Schmelter Sep 11 '14 at 21:08
  • Oh okay! Thanks alot for your time and your answer! @Tim Schmelter – user4032450 Sep 11 '14 at 21:11
  • To write numbers to console: `Console.WriteLine(string.Join(" ", numbers.Select(x=>x.ToString()))` – brz Sep 11 '14 at 22:06

4 Answers4

4

Do I need to make the user input the values into an Array which I then need to sort and then write out the sorted array?

Yes, it would be much much simpler if you do that.Just store the user inputs into an array then sort it

var numbers = new int[] { number1, number2, number3 };

Array.Sort(numbers);

int lowestNumber = numbers[0];
int middle = numbers[1];
int highest = numbers[2];

This can easily be extended if you expect more than three inputs from the user.

Selman Genç
  • 94,267
  • 13
  • 106
  • 172
2

Try using an array or a list to store all your numbers. Here's an example of how you could do it using Linq to order the list.

var myNumbers = new List<int>();
myNumbers.Add(45);
myNumbers.Add(14);
myNumbers.Add(30);
//etc
var sortedList = myNumbers.OrderBy(x => x).ToList();
//sortedList now contains a list of numbers in order from large to small.

var middleNumber = sortedList[1]; //get the second entry.
DLeh
  • 21,934
  • 14
  • 72
  • 111
  • Just finish it up by getting the middle number. (not voting) – gunr2171 Sep 11 '14 at 20:20
  • 1
    Good try, but there are some errors in your answer. The variable `sortedList` actually points to a query object that will return the numbers (in ascending order, contrary to spec) when enumerated. You can't use the `[1]` indexer on that object. You could call `.ToList()`, however :) – phoog Sep 11 '14 at 20:21
  • i didn't answer the entire question at first because it felt a lot like a homework question and I wanted to help him along without spoonfeeding. – DLeh Sep 11 '14 at 20:22
  • @DLeh fair enough. I took a downvote recently for apparenly the same reason. – phoog Sep 11 '14 at 20:23
  • Also the title led me to believe that all he wanted to do was sort :) – DLeh Sep 11 '14 at 20:24
  • @DLeh: you could make it compile by either using `ElementAt` instead of the indexer or by materializing the query into a collection, for example by using `ToList()`. – Tim Schmelter Sep 11 '14 at 20:26
  • @TimSchmelter thanks -- I didn't think of ElementAt. – phoog Sep 11 '14 at 20:27
0

Just to be different, I'm going to supply an answer that does not use an array so that there's some contrast with using an array. Also to answer the question "is it possible" (yes, yes it is).

int minimum, middle, maximum; // Use whatever numerical type is appropriate here.

if( number1 < number2 )
{
    if( number2 < number3 )
    {
        minimum = number1;
        middle = number2;
        maximum = number3;
    }
    else if( number1 < number3 )
    {
        minimum = number1;
        middle = number3;
        maximum = number2;
    }
    else
    {
        minimum = number3;
        middle = number1;
        maximum = number2;
    }
}
else
{
    if( number1 < number3 )
    {
        minimum = number2;
        middle = number1;
        maximum = number3;
    }
    else if( number2 < number3 )
    {
        minimum = number2;
        middle = number3;
        maximum = number1;
    }
    else
    {
        minimum = number3;
        middle = number2;
        maximum = number1;
    }
}
Kyle
  • 5,500
  • 2
  • 24
  • 37
  • It would be much more readable to do a "bubble sort" in a if-chain, if you're going the if-way. – Mephy Sep 11 '14 at 21:21
0

Here is another approach using a SortedSet<int> which does not contain duplicates. But that is not necessary anyway to determine the lowest, middle and maximum number:

var numbers = new SortedSet<int>();
numbers.Add(16); numbers.Add(3); numbers.Add(45);
Console.WriteLine(" {0} {1} {2}", 
    numbers.Max,                           
    numbers.ElementAt(1),// use numbers.Count/2 instead of 1 to determine the middle progr.
    numbers.Min);                         
Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859