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..
The easiest way to sort is with the sorted(list) function, which takes a list and returns a new list with those elements in sorted order. The original list is not changed. It's most common to pass a list into the sorted() function, but in fact it can take as input any sort of iterable collection.
The Sort block ranks the values of the input elements along each channel (column) in an Ascending or a Descending order, based on the Sort order you specify. Complex inputs are sorted by their magnitude, which is the sum of the squares of the real and imaginary components of the input.
first – is the index (pointer) of the first element in the range to be sorted. last – is the index (pointer) of the last element in the range to be sorted. For example, we want to sort elements of an array 'arr' from 1 to 10 position, we will use sort(arr, arr+10) and it will sort 10 elements in Ascending order.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With