-4

Edited:

I was using LINQ, to get rid of the 0 but, I just found out that I'm not allowed to use it (yes homework). Well, now I can't figure out why, when I input 0, the program still adds it to the array of numbers. Any suggestions?

static void Main(string[] args)
    {
        int first, second;
        first = second = int.MaxValue;
        int[] numbers = new int[10];
        Console.WriteLine("Enter 2-10 numbers, end early with 0: ");
        for (int i = 0; i < numbers.Length; i++)
        {
            int.TryParse(Console.ReadLine(), out int input);
            if (input == 0) break;
            numbers[i] = input;
        }
        if (numbers.Length < 2)
        {
            Console.Write("Please enter atleast 2 numbers");
            goto finish;
        }


        Small(first, second, numbers);
        finish:
        Console.ReadLine();
    }
    private static void Small(int first, int second, int[] numbers)
    {
        for (int i = 0; i < numbers.Length; i++)
        {
            if (numbers[i] < first)
            {
                second = first;
                first = numbers[i];
            }
            else if (numbers[i] < second && numbers[i] != first)
            {
                second = numbers[i];
            }
        }
        if (second == int.MaxValue)
        {
            Console.Write("Please Enter At least 2 Diffrent Numbers");

        }
        else
        {
            Console.Write("Second Lowest Number is {0}", second);
        }
    }

The output is now fine.

SynXey
  • 7
  • 1
  • You want `for(int i=0; i – juharr Dec 08 '18 at 17:14
  • You say the program "messes up?" What do you mean by this? Could you share the output? – Patrick Tucci Dec 08 '18 at 17:16
  • Try debugging the program by putting a breakpoint in the first `foreach` loop and step through the code to see what happens. – pkatsourakis Dec 08 '18 at 17:17
  • 1
    `the whole program messes up` is a bad problem description. Also, you dont need a new account for each post. Like the other post, this one would benefit from **[Navigating through Code using the Step Debugger](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx)** – Ňɏssa Pøngjǣrdenlarp Dec 08 '18 at 17:21
  • Same homework: https://stackoverflow.com/questions/53672342/how-do-i-remove-the-0-from-and-array/53679854#53679854 – Aldert Dec 08 '18 at 17:31

2 Answers2

2

First of all your iteraion is quite wired. You iterate over an array using

foreach(int i in numbers)

so you get the value of i in at all 10 positions. This number is allways unset (probably 0) so you always write to numbers[0] Using

for(int i = 0; i < numbers.Length; i++)

will solve that problem.

Second: You don't go back to your input if the user does not input enough information. Also the check

if(numbers.Length < 2)

is always true (Fixed size of 10)!

I would suggest the following:

List<int> numbers = new List<int>();
Console.WriteLine("Enter 2-10 numbers, end early with 0:");
while(numbers.Count < 10){
      int.TryParse(Console.ReadLine(), out int input);
      if (input == 0) break;
      numbers.Add(input);
}

if(numbers.Count < 2){
     Console.WriteLine("Input at least 2 numbers!");
} else {
     Small(first, second, numbers);
}
FloriUni
  • 346
  • 2
  • 10
0

your Small() method is okay and working formally correct!

But you messed up with the Main() input method.

keep your code simple to test:

static void Main(string[] args)
{
    int first, second;
    first = second = int.MaxValue;
    int[] numbers = new int[10] {3,4,5,51,3,7,10,4,5,6};

    Small(first, second, numbers);

}

this should do the same job a bit nicer:

int[] numbers = new int[10] { 3, 4, 4, 51, 3, 7, 10, 4, 5, 6 };
Console.WriteLine($"2nd smallest is {numbers.Where(n => n > numbers.Min()).First()} and {numbers.Min()} is smaller ");
Falco Alexander
  • 3,092
  • 2
  • 20
  • 39