Oskar Mikael
Oskar Mikael

Reputation: 326

C# output the lowest number of an array

My task is to let a user input numbers, and when they input 0, the loop will end and should display the lowest number. However since they're inputting 0, that becomes the lowest number. I have tried using the OrderBy to skip the first number in the array, but that doesn't seem to be working.

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Task 7\n");


            int[] numbers = new int[100];
            for(int i = 0; i < numbers.Length; i++)
            {
                Console.WriteLine("Input any number. Once you enter 0, the application will end");
                numbers[i] = int.Parse(Console.ReadLine());
                if(numbers[i] == 0)
                {
                    break;
                }
            }

            int lowest = numbers.OrderBy(num => num).Skip(1).First();
            
             Console.WriteLine($"The lowest number was: {lowest}");
             Console.ReadLine();


        }
    }

Maybe there's a smarter way overall to end the loop when the user inputs 0

Any ideas?

Upvotes: 0

Views: 1104

Answers (5)

mjb
mjb

Reputation: 136

The issue is that you are declaring int[] numbers = new int[100]; the default value of an int array is 0 therefore if for example you only enter 5 values you have the other 95 values still initialised to 0. when you sort the array you have all the zeros at the beginning.

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

Use a temporary variable to store the output from int.Parse() before adding it to your array:

var temp = int.Parse(Console.ReadLine());
if(temp == 0)
{
    break;
}
numbers[i] = temp;

You'll still get 0 as the lowest value, because the remaining indexes in the array has not been assigned to (int initializes to 0), so you might want to take that into account by either filtering the array before ordering:

int lowest = numbers.Where(num => num > 0).OrderBy(num => num).First();

Or by using a dynamically sized data structure, such as a list:

List<int> numbers = new List<int>();
for(int i = 0; i < 100; i++)
{
    Console.WriteLine("Input any number. Once you enter 0, the application will end");
    var temp = int.Parse(Console.ReadLine());
    if(temp == 0)
    {
        break;
    }
    numbers.Add(temp);
}

int lowest = numbers.OrderBy(num => num).First();

This of course assumes you want to store all the input values, otherwise just keep track of the lowest value in a single int

Upvotes: 3

Etienne Chanudet
Etienne Chanudet

Reputation: 56

if you don't need to keep all the input values, you can use an int variable to keep the lowest value between this one and the new input value. So at the end of the loop you have to print this variable which contains the lowest value.

int lowest = Int32.MaxValue;
for(int i = 0; i < numbers.Length; i++)
{
   Console.WriteLine("Input any number. Once you enter 0, the application will end");
   lowest = Math.Min(int.Parse(Console.ReadLine()), lowest);
   if(lowest == 0)
   {
      break;
   }
}

Console.WriteLine($"The lowest number was: {lowest}");
Console.ReadLine();

Upvotes: 2

persian-theme
persian-theme

Reputation: 6638

try this too

int[] numbers = new int[100];
int x = 0;
for (int i = 0; i < numbers.Length; i++)
{
     Console.WriteLine("Input any number. Once you enter 0, the application will end");   
     switch (x = int.Parse(Console.ReadLine()))
     {
         case 0:
            break;
         default:
            numbers[i] = x;
            break;
     }
     if (x == 0)
        break;
}

int lowest = numbers.OrderBy(num => num).First();

Console.WriteLine($"The lowest number was: {lowest}");
Console.ReadLine();

Upvotes: 0

Ulugbek Umbarov
Ulugbek Umbarov

Reputation: 9

Put a value to the array after checking:

var number = int.Parse(Console.ReadLine());

if (number == 0) break;

numbers[i] = number;

Upvotes: 0

Related Questions