J
J
JunikZoo2020-06-05 13:30:19
C++ / C#
JunikZoo, 2020-06-05 13:30:19

Why would the "if (j == TB_1.ColumnCount-k-1)" condition be checked for each "j"?

Completed the task "Transform the matrix by replacing the aij element with the arithmetic mean among the elements located in the i-th row and having a column number >= j". Here's how I did it:

int[,] m = new int[TB_3.RowCount,TB_3.ColumnCount];
            for (int i = 0; i < TB_3.RowCount; i++)
            {
                for (int j = 0; j < TB_3.ColumnCount; j++)
                {
                    m[i, j] = Convert.ToInt32(TB_3[j, i].Value);
                    
                }
            }
            for (int i = 0; i < TB_3.RowCount; i++)
            {
                for (int k = 0; k < TB_3.ColumnCount; k++)
                {
                    double s = 0;
                    for (int j = 0; j < TB_3.ColumnCount-k; j++)
                    {
                        s += m[i, k + j];
                        if (j == TB_3.ColumnCount-k-1)
                        {
                            double sum = s / (j + 1);
                            TB_2[k, i].Value = sum;
}

TB_3-original matrix, TB_2-transformed.
The teacher said: "calculation of the new value of the element was performed incorrectly (there are 2 errors)
- besides, why should the condition "if (j == TB_1.ColumnCount-k-1)" be checked for each "j". After all, it is known that it will be executed only once. Think about how to get rid of it completely."
Help me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JunikZoo, 2020-06-05
@JunikZoo

int[,] m = new int[TB_3.RowCount,TB_3.ColumnCount];
            for (int i = 0; i < TB_3.RowCount; i++)
            {
                for (int j = 0; j < TB_3.ColumnCount; j++)
                {
                    m[i, j] = Convert.ToInt32(TB_3[j, i].Value);
                    
                }
            } 
            for (int i = 0; i < TB_3.RowCount; i++)
            {
                for (int k = 0; k < TB_3.ColumnCount; k++)
                {

                    double s = 0;
                    
                    for (int j = 0; j < TB_3.ColumnCount-k; j++)
                    {
                        
                        s += m[i, k + j];
                       }
                        
                            double sum = s / (j + 1);
                            TB_2[k, i].Value = sum;
                        
                    }
                }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question