M
M
Michael2020-05-24 13:13:14
C++ / C#
Michael, 2020-05-24 13:13:14

Need help with problem solving. It is necessary to calculate the change of signs. What's my mistake?

private void button7_Click(object sender, EventArgs e)
        {
            int s = 0;
            dataGridView1.RowCount = 3;
            dataGridView1.ColumnCount = 5;
            Random r = new Random();
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    dataGridView1.Rows[i].Cells[j].Value = r.Next(-50, 50);
                }
            }
            int[,] array = new int[3,5];
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    array[i,j] = (int)dataGridView1.Rows[i].Cells[j].Value;
                   if (array[i,j] < 0 && array[i++,j++] >= 0 || array[i,j] >= 0 && array[i++,j++] < 0) //Пишет — «Индекс находится вне границ массива»
                    {
                        s++;
                    }
                    label23.Text = Convert.ToString(s);
                }
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ayazer, 2020-05-24
@ayazer

well, run the debugger and see. there is more than 1 mistake.
1) you can start checking from the option when i==2 and j==4. then array[i++,j++] is already out of bounds
2) array[i++,j++] - are you sure that you need to compare values ​​diagonally?
3) i++ is a post-increment that changes the value. I suspect that you just wanted to compare with the following
4)

array[i,j] < 0 && array[i++,j++] >= 0 || array[i,j] >= 0 && array[i++,j++] < 0
run it in a debugger/interactive console and see if it really works as you thought. I suspect that you wanted to get a different result though

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question