I am trying to create a program in windows forms and i am almost done by my array will not store numbers. It will only use the last number entered. Please help, i did this program in c sharp without a problem but cant figure out windows forms application.

public partial class Form1 : Form
    {
        int[] vars=new int[5];
        int integersEntered;

        public Form1()
        {
            InitializeComponent();



        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            vars[4] = int.Parse(NosArray.Text);
            Integer.Text = integersEntered.ToString();           

            integersEntered++;

            NosArray.Text = String.Empty;
            NosArray.Focus();



        }

        private void Integer_TextChanged(object sender, EventArgs e)
        {


        }

        private void NosArray_TextChanged(object sender, EventArgs e)
        {


        }

        private void Var_Click(object sender, EventArgs e)
        {
            float avg = (vars[0] + vars[1] + vars[2] + vars[3] + vars[4] / 5);

            OutputLabel1.Text = " the average of the numbers " + vars[0] + ", " + vars[1] +
                  ", " + vars[2] + ", " + vars[3] + " and " + vars[4] + " is " +
                   avg.ToString();

            OutputLabel1.Visible = true;
            OutputLabel2.Visible = true;
            Avg.Visible = true;
            Var.Visible = true;
            float Variance = ((((((((((vars[0]) - avg) * (vars[0]) - avg)) + ((vars[1]) - avg) * (vars[1]) - avg)) + ((vars[2]) - avg) * (vars[2]) - avg)) + ((vars[3] - avg) * (vars[3] - avg)) + ((vars[4]) - avg) * (vars[4]) - avg) / 4);
            OutputLabel2.Text = "The Variance is " + Variance.ToString();
        }

        private void Avg_Click(object sender, EventArgs e)
        {

        }
    }
}

Recommended Answers

All 8 Replies

I guess the scenerio is not clear! From where you are getting input for vars[0], vars[1], vars[2] and vars[3].

That's the problem. I have a text box and a button that should add numbers to an array. I put a number in the textbox and push the add to array button. It will only take the last number entered but I need five numbers in the array. I don't need var 1 or 2 or 3 or 4. I was just wondering how to get five numbers to the array.

So the problem is with your following code snippet:

vars[4] = int.Parse(NosArray.Text);
Integer.Text = integersEntered.ToString();
integersEntered++;
NosArray.Text = String.Empty;
NosArray.Focus();

where you are getting only var[4] but not var[0], var[1], var[2] and var[3] which you want to enter after every successive button press. Have I got it ?

Also what is Integer.Text's purpose ?

One peace of advice please make your code readable with appropriate variable name's and method names. It becomes really difficult to understand even a simple program like this if it is not coded in a readable mannner.

Yes that's it. sorry about Unreadable code. I'm no good at windows forms. integer.text is a text box that counts and displays the number of entries in the array. This part works. vars is the name of my array. I am supposed to have a user enter five numbers one at a time. After each number is entered the add to array button is pushed. After five numbers are entered the average and variance is automatically calculated and displayed. If I can get the array to work I think I can get the rest to work. Thank you so much for helping me with this.

Try this:

int integersEntered = 0;//Just to ensure initialization though implicitly it's initialized to zero. But it's a good programming practice.
.
.
.
.
.
.
private void button1_Click(object sender, EventArgs e)
{
    if(integersEntered<5)//Ensures array out of bound exception will not occur as you have created a array of 5 elements
    {
        vars[integersEntered] = int.Parse(NosArray.Text);
        integersEntered++;
        Integer.Text = integersEntered.ToString();
        NosArray.Text = String.Empty;
        NosArray.Focus();
    }
}

Here's a problem with the code. What if you entered all 5 integers and then again want to reset ? in that case before assigning any data you can use Array.Clear to clear the array and do that at the else part of the if(integersEntered<5) and also reset integersEntered to zero.

I hope that will help.

http://stackoverflow.com/questions/13757390/how-to-empty-the-array-in-c

On line 47 you are doing integer division!
so if vars[0] + vars[1] + vars[2] + vars[3] + vars[4] for example equals 7, this would give: 7/5 = 1 for your average.
Solution: If you divide by 5.0, this would give the correct 1.4
Also: why dont you use for statements? Have a look here on how you could use a for statement for the calculation of average and variance.

Yes thanks so much I got it working now.

Thank you so much it works now. Thank you for the link also.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.