taji01 - 1 year ago 141
C# Question

Get the largest and smallest number from a array

I’m using

`WinForms`
. At first I was having a hard time adding data from textbox to an array, but I figured it out, but I don’t know if this is the ideal way to do it. Next, what I’m trying to do is get the largest number in the array and the smallest number in the array except of zero. In my example, I typed in 2, 5, 8, 7, 6 7. For this example, my goal is to get the numbers 2 and 7 back. 2 being the smallest number in the array besides 0, and 7 being the largest number in the array

So far i have this.

``````private const int MAX_ITEMS = 10;
private int[] numArray = new int[MAX_ITEMS];
private int index = 0;

private void Add_Data_Btn_Click(object sender, EventArgs e)
{
if (this.index < numArray.GetUpperBound(0))
{
int dnum;
if (int.TryParse(i_richTextBox1.Text, out dnum))
{
numArray[this.index++] = dnum;
i_richTextBox1.Text = "";
}

if (Value_Out_Array_CheckBx.Checked == true)
{
foreach (var item in numArray)
{
Console.WriteLine(item);

}
}
}
}
``````

After understanding that what you want is to get one number at each press of the button and that each time you want to calculate the Min and Max I suggest:

``````private List<int> _numbers = new List<int>(); // Use List<int> to avoid having irrelevant 0 items

private int _min;
private int _max;

private void Add_Data_Btn_Click(object sender, EventArgs e)
{
//Parse and add new number to collection (notice - this does not take care of invalid input..
var number = int.Parse(i_richTextBox1.Text);

// If it is the first time iterating then this number is both the _min and _max
if(_numbers.Count == 0)
{
_min = number;
_max = number;
}
else
{
//Check if need to update _min or _max
if(number < _min)
_min = number;
else if(number > max)
_max = number;
}

}
``````

Of course you can still use linq `.Min` and `Max` operations on the collection to check for the updated values but why do an `o(2n)` when you can `2x o(n)`

To deal with the `0` problem:

You can use Linq `Except` or check that the item isn't `0` when looking for Min and Max but I'd just suggest changing from an `int[10]` to a `List<int>` - That way you won't have those empty items.

If it is a list then each time the button is pressed:

``````private void Add_Data_Btn_Click(object sender, EventArgs e)
{
}
``````

Getting Min and Max:

Use linq:

``````List<int> numbers = new List<int> {2, 5, 8, 7, 6 7};
var min = numbers.Min();
var max = numbers.Max();
``````

Or if you want to do it in `o(n)` instead of `o(2n)`:

``````List<int> numbers = new List<int> {2, 5, 8, 7, 6 7};
int min = numbers[0];
int max = numbers[0];

foreach(item in numbers)
{
if(item > max)
max = item;
else if(item < min)
min = item;
}
``````

Change way of getting numbers:

If you want to change the way you get the data from the `TextBox` and get it all at once (instead of one number at a time) then use `string.Split`:

``````string data = "2 5 8 7 6 7";
var numbers = data.Split(' ')
.Select(int.Parse).ToList();

// Or if you might have more spaces:
string data = "2  5 8 7   6 7";
var numbers = data.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse).ToList();
``````

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download