A
A
Aveqt2021-09-30 18:24:32
C++ / C#
Aveqt, 2021-09-30 18:24:32

How to form a one-dimensional array from matrix elements that are below the main diagonal and greater than any element located above?

The task sounds like this: Given a square matrix of order n. Form a one-dimensional array consisting of those elements that are below the main diagonal and greater than any of the elements located above the main diagonal. If there are no such elements, display "There are no searched elements" in the textbox. To generate random values, use the following parameters: n in the range of 3...6, matrix values ​​in the range of -10
...
35

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            dataGridView1.RowCount = Convert.ToInt32(numericUpDown1.Value);
            dataGridView2.RowCount = Convert.ToInt32(numericUpDown1.Value);
            dataGridView1.ColumnCount = Convert.ToInt32(numericUpDown1.Value);
        }

        private void Main_F_Load(object sender, EventArgs e)
        {
            dataGridView1.RowCount = Convert.ToInt32(numericUpDown1.Value);
            dataGridView2.RowCount = Convert.ToInt32(numericUpDown1.Value);
            dataGridView1.ColumnCount = Convert.ToInt32(numericUpDown1.Value);
            dataGridView2.ColumnCount = 1;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            double[,] matrA = new double[dataGridView1.RowCount, dataGridView1.ColumnCount];
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                for (int j = 0; j < dataGridView1.ColumnCount; j++)
                    matrA[i, j] = Convert.ToDouble(dataGridView1[j, i].Value);
            }
            double [,] mas = new double [dataGridView1.RowCount, dataGridView1.ColumnCount];
            double Max = mas[0, 1];
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                for (int j = 0; j < dataGridView1.ColumnCount; j++)
                {
                    if (j > i)
                    {
                        if (mas[i, j] > Max)
                        {
                            Max = mas[i, j];
                        }
                    }
                }
            }
            int count = 0;
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                for (int j = 0; j < dataGridView1.ColumnCount; j++)
                {
                    if (j < i)
                    {
                        if (mas[i, j] > Max)
                        {
                            count++;
                        }
                    }
                }
            }



            if (count == 0)
            {
                dataGridView2[0, 0].Value = "Искомых элементов нет";
            }

            else
            {
                double[] res = new double[count];
                int index = 0;
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    for (int j = 0; j < dataGridView1.ColumnCount; j++)
                    {
                        if (j < i)
                        {
                            if (mas[i, j] > Max)
                            {
                                res[index] = mas[i, j];
                                index++;
                            }
                        }
                    }
                }

                for (int i = 0; i < count; i++)
                {
                    dataGridView2[0, i].Value = res[i];
                }
            }









        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (BinaryWriter bw = new BinaryWriter(File.Open("source.dat", FileMode.Create)))
            {
                int row = Convert.ToInt32(numericUpDown1.Value);
                bw.Write(row);
                int col = Convert.ToInt32(numericUpDown1.Value);
                bw.Write(col);
                for (int i = 0; i < row; i++)
                    for (int j = 0; j < col; j++)
                    {
                        bw.Write(Convert.ToDouble(dataGridView1[j, i].Value));
                    }
            }
            MessageBox.Show("Запись успешно выполнена", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (BinaryReader br = new BinaryReader(File.Open("source.dat", FileMode.Open)))
            {
                int r = br.ReadInt32();
                int c = br.ReadInt32();
                numericUpDown1.Value = r;
                numericUpDown1.Value = c;
                for (int i = 0; i < r; i++)
                    for (int j = c; j < c; j++)
                    {
                        double num = br.ReadDouble();
                        dataGridView1[j, i].Value = num;
                    }
            }
            for (int i = 0; i < dataGridView1.RowCount; i++)
                for (int j = 0; j < dataGridView1.ColumnCount; j++)
                    dataGridView2[j, i].Value = "";



        }

        private void button5_Click(object sender, EventArgs e)
        {
            using (StreamWriter sw = new StreamWriter(File.Open("result.txt", FileMode.Create)))
            {
                for (int i = 0; i < dataGridView2.RowCount; i++)
                    for (int j = 0; j < dataGridView2.ColumnCount; j++)
                        sw.WriteLine(dataGridView2[j, i].Value.ToString());
            }
            MessageBox.Show("Результат сохранен", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Random rnd = new Random();
            numericUpDown1.Value = rnd.Next(3, 7);
            for (int i = 0; i < dataGridView1.RowCount; i++)
                for (int j = 0; j < dataGridView1.ColumnCount; j++)
                    dataGridView1[j, i].Value = rnd.Next(-10, 36);
            for (int i = 0; i < dataGridView1.RowCount; i++)
                for (int j = 0; j < dataGridView1.ColumnCount; j++)
                    dataGridView2[j, i].Value = "";
        }
    }
}

6155d69d5cfcf271097800.png
List of errors:6155d653aa997193045235.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question