Home > Software engineering >  Program that creates an array of elements obtained from actions on the first matrix and outputs the
Program that creates an array of elements obtained from actions on the first matrix and outputs the

Time:12-26

Create a program that finds all elements in the matrix D(m, n), where the sum of all the elements of the row standing before the one under consideration is greater than the sum of the elements of the column standing before the one under consideration. The sum of the preceding elements is considered equal to zero if the element is the first in a row or column. Form an array from the found elements. Output the matrix as a matrix, and below it output the elements of the array.(Windows Forms application)

It turns out to create the first matrix. I can't create an array according to a given rule. And I can't figure out how to output array elements(not console! its Windows Forms app)

I have been suffering for a week with this task.

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int M = 0;
        int N = 0;
        int[,] Numbers;
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e) // create matrix
        {
            dataGridView1.Columns.Clear();
            dataGridView1.Rows.Clear();


            dataGridView1.AllowUserToAddRows = true;

            M = int.Parse(M_input.Text);
            N = int.Parse(N_input.Text);

            Numbers = new int[0, 0];
            Numbers = new int[N, M];

            for (int i = 0; i < M; i  )
            {
                dataGridView1.Columns.Add("", "");
            }

            for (int i = 0; i < N; i  )
            {
                dataGridView1.Rows.Add("", "");
            }

            dataGridView1.AllowUserToAddRows = false;
        }
        //this button should create an array and output array elements (in the form of a string, most likely)
        private void button2_Click(object sender, EventArgs e)
        {
            int n = dataGridView1.RowCount;
            int m = dataGridView1.ColumnCount;
            double[,] array1 = new double[n, m];

            for (int i = 0; i < n; i  )
                for (int j = 0; j < m; j  )
                {
                    array1[i, j] = double.Parse(dataGridView1.Rows[i].Cells[j].Value.ToString());

                }
            int times = 0;
            dataGridView2.RowCount = 1;
            dataGridView2.ColumnCount = 0;

            for (int i = 0; i <= n; i  )
                for (int j = 0; j <= m; j  )

                {
                    double sum1 = 0;
                    double sum2 = 0;

                    if (i == 0)
                        sum1 = 0;
                    else
                        for (int k = 0; k < i; k  )
                            sum1  = array1[i, k];

                    if (j == 0)
                        sum2 = 0;
                    else
                        for (int k = 0; k < j; k  )
                            sum2  = array1[k, j];


                    if (sum1 > sum2)
                    {

                        dataGridView2.ColumnCount  ;
                        dataGridView2.Rows[0].Cells[times].Value = array1[i, j];
                        times  ;
                    }
                }
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void LUM1_Click(object sender, EventArgs e)
        {

        }

        private void LUM2_Click(object sender, EventArgs e)
        {

        }

        private void LUM3_Click(object sender, EventArgs e)
        {

        }

        private void Matrix_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void M_input_TextChanged(object sender, EventArgs e)
        {

        }

        private void N_input_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void Matrix2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}

CodePudding user response:

Imagine you have excel open, and you have numbers from 1 to 100 in a 10x10 grid.. A1 is 1, B1 is 2, A2 is 11 etc

Your assignment asks you to pick a cell, say B2. Is the sum of the values in row 1 greater than the sum of the values in column A? Yes-> put the value of B2 in a list. Repeat for another cell. Do all the cells

I started on B2 because it has a row above it and a column to the left of it so it doesn't need to handle that special case of "no row or column means means sum is 0"'that you get for any cell on row 1 or in column A


  • Make your life easy. Write two Methods:
    public int SumColumn(int whichCol)
      //if whichCol is -1 return 0

      //declare variable to hold the sum
      //loop over the grid from [row 0, col whichCol] to [row N, col whichCol], adding up into sum
      //return sum

    public int SumRow(int which)
      //copy paste and adapt code above to work across rows not down columns
  • Now check your grid
   //declare a list to hold the cells we find with SumRow>SumColumn 
   //for r in rows
     //for c in columns 
       //add current cell value to a textbox ("output the matrix")
       //if SumRow(r-1) > SumColumn(c-1) 
         //add to list

    //add the contents of the list to the TextBox too with another loop

Should take about 15 minutes to write this code, not weeks. The design process for code when you're unfamiliar with anything is like I have done here. Write comments in the language you think in; get the algorithm straight in your mind and written down before you start hacking out code and getting lost. The comments are your direction, your essay plan, the recipe book while everything in the kitchen is going crazy. You absolutely have to plan your programs just like when you speak a foreign language; first you imagine the sentence you want to say in your native language, then maybe you rearrange it to how the foreigners say it (word order), then you translate the words and conjugate, modify etc, then finally you speak it. Engaging in that translation process for English->C# is the same; you know one language but not the other

When you're done, leave the comments in so if you went wrong you can either see it and fix it or your supervisor can see where your thinking and your c# understanding diverged, give you points for the algorithm and know what to teach you to help with where it went wrong

CodePudding user response:

I have solved the problem Image of matrix

namespace Matrix
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int m; // columns
        int n; // rows

        int[,] MatrixArray; // array of matrix elements 

        private void Create_Click(object sender, EventArgs e)// By clicking, a matrix grid is created
        {
            GridMatrix.AllowUserToAddRows = true;

            m = int.Parse(Size_M.Text); // Enter the number of columns 
            n = int.Parse(Size_N.Text); // Enter the number of rows

            _Answer.Text = "Elements (answer):";

            GridMatrix.Columns.Clear();
            GridMatrix.Rows.Clear();

            MatrixArray = new int[n, m]; // Creating an empty array of dimension m x n


            for (int i = 0; i < m; i  )// Creating columns
            {
                GridMatrix.Columns.Add("", "");
            }
            for (int i = 0; i < n; i  )// Creating rows
            {
                GridMatrix.Rows.Add("", "");
            }

            GridMatrix.AllowUserToAddRows = false;
        }

        private void Answer_Click(object sender, EventArgs e)// When pressed, the answer appears
        {
         
            for(int i = 0; i < n; i  )// Transferring a matrix to an array
            {
                for(int j = 0; j < m; j  )
                {
                    MatrixArray[i, j] = int.Parse(GridMatrix[j, i].Value.ToString());
                }
            }

            int[] AnswerArray=new int[m*n];
            int Counter=0;

            for (int i = 0; i < n; i  ) // The algorithm for finding elements
            {
                for (int j = 0; j < m; j  )
                {
                    int RowSumm = 0,ColumnSumm=0; // Sums of row and column elements

                    for (int b=0;b<j;b  )// Sum of rows
                    {
                        RowSumm  = MatrixArray[i, b];
                    }
                    for(int b = 0; b < i; b  )// Sum of columns
                    {
                        ColumnSumm  = MatrixArray[b, j];
                    }

                    if (RowSumm>ColumnSumm)
                    {
                        AnswerArray[Counter] = MatrixArray[i, j];
                        Counter  ;
                    }
                }
            }

            _Answer.Text = "Elements (answer):";

            for (int i=0;i<Counter;i  )// Output of array elements in turn
            {
                _Answer.Text  = "" AnswerArray[i];

                if (i!=Counter-1)
                {
                    _Answer.Text  = ", ";
                }
            }

        }

        private void M_Label_Click(object sender, EventArgs e)
        {

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

        }

        private void Answ1_Click(object sender, EventArgs e)
        {

        }

        private void GridMatrix_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void SizeLabel_Click(object sender, EventArgs e)
        {

        }

        private void Size_M_TextChanged(object sender, EventArgs e)
        {

        }

        private void Size_N_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
  • Related