1
private void button1_Click(object sender, EventArgs e)
{
    int[] m = new int[1000];
    int n = textBox1.Lines.Lenght;
    int i;
    int k = 0;
    int sum = 0;
    int product = 1;
    int average = 0;

    for (i = 0; i < n; i++)
    {
        try 
        {
            m[k] = Convert.ToInt32(textBox1.Lines[i]);
            sum = sum + m[k];
            product = product * m[k];
            average = sum / n;
        }     
        catch 
        {
            MessangeBox.Show("Буквы нельзя!!");
            k++;
        }
    }

    label10.Text = n.ToString();
    label11.Text = sum.ToString();
    label12.Text = product.ToString();
    label13.Text = average.ToString();     
}
2
  • your question is little bit unclear. can you explain your problem more? Commented Apr 14, 2016 at 18:39
  • Use Max and Min to find the largest number or smallest number in your array. Commented Apr 14, 2016 at 19:20

2 Answers 2

1

The following should help:

private void button1_Click(object sender, EventArgs e)
{
    int[] m = new int[1000];
    int n = textBox1.Lines.Lenght;
    int i;
    int k = 0;
    int sum = 0;
    int product = 1;
    int average = 0;
    int min = Int32.MaxValue;   // will hold min value
    int max = Int32.MinValue;   // will hold max value

    for (i = 0; i < n; i++)
    {
        try 
        {
            m[k] = Convert.ToInt32(textBox1.Lines[i]);
            sum = sum + m[k];
            product = product * m[k];
            if (m[k] < min)
              min = m[k];
            if (m[k] > max)
              max = m[k];
        }     
        catch 
        {
            MessageBox.Show("Буквы нельзя!!");
            k++;
        }
    }
    average = sum / n;  //Computing average here is more efficient
    label10.Text = n.ToString();
    label11.Text = sum.ToString();
    label12.Text = product.ToString();
    label13.Text = average.ToString();     
}

Caveat: If your array is empty, you may not want to have Int32.MaxValue and Int32.MinValue as your default min and max. You will need to handle that case separately.

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

1 Comment

This is the most efficient solution, I think.
1
using System;

using System.Collections.Generic;

using System.Linq;



int[] m = new int[1000];

// fill in the array here .....

int min = m.Min(); // find the minimum

int max = m.Max(); // find the maximum

3 Comments

care to explain why there are down votes? He wants to know how to find minimum and maximum of the array.
I agree, that's the sort of answer I would be looking for!
No idea why it was downvoted. Have an upvote. :) However, it would be more efficient to compute the max and min in the existing loop in the OP's code rather than loop through all the elements an extra two times.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.