0

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?

4
  • One (easy) way would be not to insert the final 0 into the array. But then, in an int array every element is initialized with 0. So unless the user enters all 100 (or whatever) numbers, there's gonna be 0s in it. But you could use a List instead of an array. Commented Feb 11, 2021 at 13:06
  • The problem is that default values of array are 0. Use another variable to count number of items user have entered (before he enters 0, which is not a problem at all to store) or as per @stickybit comment, use a List<int>. Commented Feb 11, 2021 at 13:07
  • Since Console.ReadLine returns a string, I'd recommend changing the terminating key to be something other than 0, say x, or something like that. Then, you change your int.Parse to TryParse and only add numbers to the array where TryParse succeeds (returns true). That way you'll have a clean array that you can then get the smallest integer for. Commented Feb 11, 2021 at 13:11
  • You shouldn't use an array at all. Either a list, or even better, don't store the values at all! Just check the entered value using a do while loop. Commented Feb 11, 2021 at 13:15

5 Answers 5

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

Filtering out all 0 is probably the easiest change to original code. Storing 0 is not a problem at all then.
@Sinatr If all values must be stored for some reason, then yes, I agree.
All the values has to be stored, because the program has to find the lowest number of all the numbers the user inputs. This solved my issue, Thanks!
@OskarB I would definitely go with a List<int> then :-)
2

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();

Comments

0

Put a value to the array after checking:

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

if (number == 0) break;

numbers[i] = number;

Comments

0

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();

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.