I'm working on a program to convert a set of values to an average and I keep getting an exception error. Seems to be an easy fix but I can't put my finger on it. Any suggestions?

private void button1_Click(object sender, EventArgs e)
    {
        int sum = 0;
        int ave = 0;
        int[] temp = new int[10];

        sum = int.Parse(txtData.Text);

        for (int i = 0; i < 10; i++)
        {
            sum = sum + temp[i];
        }

        ave = sum / 10;
        txtAve.Text = ave.ToString();

    }
    
	
}

I'm not sure what the policy here on Double posts is but since I figured out my own problem I figure I'd post a solution for anyone else out there....

private void button1_Click(object sender, EventArgs e)
    {
        //Define variables
        //Instantiate array
        double sum = 0;
        double ave = 0;
        string[] data = new string[10];
        char [] delim = { ',' };
        data = txtData.Text.Split(delim);
        
        //if statement if you go over 10 items
        if (data.Length > 10)
        {
            MessageBox.Show("Only 10 numbers seperated by commas allowed.",Text,MessageBoxButtons.OK,MessageBoxIcon.Error);
            txtData.Clear();
            txtData.Focus();
            txtAve.Clear();
        }

        //exception handling
        try
        {
            for (int i = 0; i < data.Length; i++)//begin loop for array to show the average
            {
                sum += double.Parse(data[i]);//calculation to run in loop
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error:" + ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
        ave = sum / data.Length;//calculating average
        txtAve.Text = ave.ToString();//Fini!


    }
    
	
}
This article has been dead for over six months. Start a new discussion instead.