0

the assignment asks me to find the second lowest number from the user input (max 10), and if a 0 is entered the program should stop and find the second lowest, without counting. But I have to declare the size of the array beforehand. For example, if I type in 4 7 3 8 0 the program also counts all the 0 until there are 10 numbers so it sees this: 4 7 3 8 0 0 0 0 0 0. Is there a way for me to stop the program from seeing the other 0 or to somehow change the array size?

Edit: Ok, so here is the code I have so far:

        int s;
        s = 10;
        int[] numbers = new int[s];

        for (int i = 0; i < numbers.Length; i++)
        {
            int.TryParse(Console.ReadLine(), out numbers[i]);

        }

        int firstLowestNumber = numbers[0];
        int secondLowestNumber = numbers[0];
        for (int i = 0; i < numbers.Length; i++)
        {
            if (numbers[i] < firstLowestNumber)
            {
                firstLowestNumber = numbers[i];
            }
        }

        for (int x = 0; x < numbers.Length; x++)
        {
            if (numbers[x] < secondLowestNumber && firstLowestNumber != numbers[x])
            {
                secondLowestNumber = numbers[x];
            }
        }

        Console.WriteLine("Second Lowest Number is {0}", secondLowestNumber);



        Console.ReadLine();
Aldert
  • 4,209
  • 1
  • 9
  • 23
  • 4
    Use a `List` not an array – Liam Dec 07 '18 at 15:20
  • 1
    If you really must use an array then you [can resize it](https://stackoverflow.com/a/4840817/542251) but no one has done this since .Net 1.1 in about 2005. `List` never needs re-sizing as it's a dynamically sized array. – Liam Dec 07 '18 at 15:22
  • also why does the fact that this is home work mean you can't show your code? – Liam Dec 07 '18 at 15:22
  • 1
    You can post homework code here. In fact, we prefer that you do so. Please share your code with us so we can better help you. –  Dec 07 '18 at 15:22
  • Possible duplicate of [change array size](https://stackoverflow.com/questions/4840802/change-array-size) – Liam Dec 07 '18 at 15:23
  • As this is homework, I'm not sure OP has been shown `List`. But you shouldn't need to resize the array or use a `List`. Based on the example input, you could just use `string.Split(' ');` to create the array. It will automatically create the correct size based on the input. Then just add a check to make sure it doesn't exceed 10 elements. – Lews Therin Dec 07 '18 at 15:23
  • This question has already been answered you can find it [here](https://stackoverflow.com/questions/496896/how-to-delete-an-element-from-an-array-in-c-sharp) – jasper vandenborne Dec 07 '18 at 15:24
  • 3
    @Liam If they posted their homework then their teacher would be able to find this question and know that they broke the school rules. If they don't, the teacher probably won't ever find this question. – Servy Dec 07 '18 at 15:28
  • @LewsTherin They are inputting one number at a time, not a string of numbers. – NetMage Dec 07 '18 at 18:29
  • You don't need to resize the `Array`, just count how many entries occurred in the `for` loop. Also, use a `while` loop or add an additional test to exit when `0` is entered, and don't count the `0` entry. – NetMage Dec 07 '18 at 18:29
  • @NetMage When I wrote my comment, they had posted no code. `For example, if I type in 4 7 3 8 0...` is written as if they entered all the numbers at once, separated by a space. That was my assumption and why I wrote my comment. OP should update the question to clarify. – Lews Therin Dec 07 '18 at 18:32
  • @LewsTherin The code plainly shows how the input is being done. Using `int.TryParse(Console.ReadLine()` indicates one number per line entry. – NetMage Dec 07 '18 at 18:33
  • @NetMage Yes, I re-read the question and saw they added code. My initial comment was prior to any code being posted by the OP. – Lews Therin Dec 07 '18 at 18:33
  • @LewsTherin To me, the "if a 0 is entered" implied the same thing :) – NetMage Dec 07 '18 at 18:35

2 Answers2

2

With the initialization of an int-array all numbers will be 0. This means you only need to stop reading when a 0 is given as input.

So a little change in the first foreach will do:

int input;
int.TryParse(Console.ReadLine(), out input);
if (input == 0) break; //This will jump out of the foreach
number[i] = input;
Aldert
  • 4,209
  • 1
  • 9
  • 23
0

It depends on goal of your homework.

1) Using of Linq (its possible teacher requested this task for you especially to not use a Linq)

using System.Linq;
/*...*/
List<int> ints = new List<int>();
ints.Add(/*...*/);
/*...*/
ints = ints.Where(x => x != 0).ToList();
/*...*/

2) As @Aldert's answer.

3) change printing mechanics of array

int[] ints = new int[10];

foreach (int element in ints) {
  if (element == 0)
    continue;
  Console.Write("{0} ", element);
}
VitezslavSimon
  • 342
  • 2
  • 7