1

I wrote a code that does a 100 random numbers between -100,100 1st button calculates average of negative numbers 2nd button calculates average of odd positive. And here is where I am stuck how can I write a code that when I click on the 3rd button and it -5 from the 1st button value. Same problem for 4th button but if you could help me with this problem I can solve the last problem by myself.

    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        int[] P1 = new int[100];
        Random rand = new Random();
        float enAvg = AvgNeg(P1);
        textBox1.Text = (enAvg).ToString("");
    }
    private void button2_Click(object sender, EventArgs e)
    {
        int[] P2 = new int[100];
        Random rand = new Random();
        float enAvg = AvgOddPos(P2);
        textBox2.Text = (enAvg).ToString("");
    }
    private void button3_Click(object sender, EventArgs e)
    {
        textBox1.Text = (Convert.ToInt32(textBox1.Text) + (-5).ToString());
    }
    private void button4_Click(object sender, EventArgs e)
    {
        textBox2.Text = (Convert.ToInt32(textBox1.Text) + (+ 10).ToString());
    }
    static float AvgNeg(int[] array)
    {
        Random rand = new Random();
        float sum = 0;
        int counter = 0;
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = rand.Next(-100, 100); 
            if (array[i] < 0)
            {
                sum += array[i];
                counter++;
            }
        }
        float avg = sum / counter;
        return avg;
    }
    static float AvgOddPos(int[] array)
    {
        Random rand = new Random();
        float sum = 0;
        int counter = 0;
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = rand.Next(-100, 100);
            if (array[i] > 0 % 1)
            {
                sum += array[i];
                counter++;
            }
        }
        float avg = sum / counter;
        return avg;
    }
4
  • 1
    Could you please clarify what is the question exactly? Commented Apr 9, 2014 at 1:50
  • When I click button 3 it should subtract -5 from the value of button 1 Commented Apr 9, 2014 at 1:53
  • On the face of it, your implementation looks correct except that you should convert to string after adding 10 or subtracting 5. Currently you are trying to add 10.ToString to an integer which wouldn't work. Commented Apr 9, 2014 at 1:54
  • @MihaiHantea Tried that it gives me this error Input string was not in a correct format. Commented Apr 9, 2014 at 1:55

2 Answers 2

2

Don't work with the TextBoxes. Use them just for display. After calculating the averages, assign the values to variables and then display the values of those variables in the TextBoxes. If you want to change the values, use the variables in the calculations and, if appropriate, update the variables and then display the results in the TextBoxes.

By the way, stop creating Random objects all over the place. You should be creating one and only one Random object. Declare a member variable of type Random and initialise it with a new instance, then use that one variable everywhere you need a random number.

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

Comments

1

declare a class level variable

    public partial class Form1 : Form
        {
                   private float value;
            public Form1()
            {
                InitializeComponent();
            value=0;
            }
        }

then store your button1-5 value inside the value variable

 private void button1_Click(object sender, EventArgs e)
        {
            int[] P1 = new int[100];
            Random rand = new Random();
            float enAvg = AvgNeg(P1);
          value=enAvg-5;
            textBox1.Text = (enAvg).ToString("");
        }


private void button3_Click(object sender, EventArgs e)
    {
        textBox1.Text = (value.ToString());
    }

here you are

1 Comment

Thanks I would have never figure that out, now I can finish the rest with ease :) thank u so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.