Error adding matrix

0

At the moment of entering a number of rows and disparate columns and pressing the button to add the program closes

    Random objnum = new Random();
    int[,] matriz;
    int numeros, suma, x, y, suma1, suma3;
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        x = Convert.ToInt16(textBox1.Text);
        y = Convert.ToInt16(textBox2.Text);
        matriz = new int[x + 1, y + 1];
        dataGridView1.RowCount = x + 1;
        dataGridView1.ColumnCount = y + 1;

        for (int i = 0; i < matriz.GetLength(0) - 1; i++)
        {

            for (int j = 0; j < matriz.GetLength(1) - 1; j++)
            {
                numeros = objnum.Next(1, 10);
                matriz[i, j] = numeros;
                dataGridView1[j, i].Value = matriz[i, j];

            }

        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < matriz.GetLength(0) - 1; i++)
        {



            suma = 0;
            suma1 = 0;
            for (int q = 0; q < matriz.GetLength(1) - 1; q++)
            {


                suma1 = suma1 + matriz[q, i];
                suma = suma + matriz[i, q];


            }

            matriz[matriz.GetLength(0) - 1, i] = suma;
            dataGridView1[matriz.GetLength(0) - 1, i].Value = matriz[matriz.GetLength(0) - 1, i];
            matriz[i, matriz.GetLength(1) - 1] = suma1;
            dataGridView1[i, matriz.GetLength(1) - 1].Value = matriz[i, matriz.GetLength(1) - 1];
            suma3 = suma3 + suma1 + suma;
            matriz[matriz.GetLength(0) - 1, matriz.GetLength(1) - 1] = suma3;
            dataGridView1[matriz.GetLength(0) - 1, matriz.GetLength(1) - 1].Value = matriz[matriz.GetLength(0) - 1, matriz.GetLength(1) - 1];


        }


    }
}}
    
asked by Joonathan Francisco 04.12.2018 в 20:21
source

0 answers