taji01
taji01

Reputation: 2615

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

            }
        }
    }
}

enter image description here

Upvotes: 2

Views: 989

Answers (3)

Gilad Green
Gilad Green

Reputation: 37299

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;
    }

    _numbers.Add(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)


Answer before understanding latest comment:

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)
{
    numberList.Add(int.Parse(i_richTextBox1.Text));
}

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

Upvotes: 8

Reza Aghaei
Reza Aghaei

Reputation: 125207

Since you want to get the minimum except 0, you can use:

var min = numArray.Except(new int[] { 0 }).Min();

or

var min = numArray.Where(x => x != 0).Min();

And for the maximum, as mentioned by Gilad too:

var max = numArray.Max();

Be aware of an InvalidOperationException saying Sequence contains no elements, if the numArray contains no element of just a 0.

Upvotes: 1

L.B
L.B

Reputation: 116138

instead of getting numbers one by one you can read all the numbers at once. And using Regex and Linq

var numbers = Regex.Matches(i_richTextBox1.Text, @"\d+")
                .Cast<Match>()
                .Select(m=>int.Parse(m.Value))
                .ToArray();
var min = numbers.Min();
var max = numbers.Max();

For ex, type 2 3 4 61 7 12 to your textbox..

Upvotes: 1

Related Questions