PLEASE HELP Sum of elements of a matrix

0

I am a student of computer engineering and I just left an exercise which is giving me several problems to solve it, I have to create a 4x5 matrix in windows forms and display all the elements of this, all right here but in addition to doing this I have to do the sum of the elements that are in the even rows and find the largest element of the middle column, here I leave what I carry in advance thanks.

    {
        int R, C;

        int[,] numeros = new int[4, 5] { { 10, 45, 7, 23, 13 }, { 8, 76, 11, 58, 14 }, { 12, 55, 43, 32, 15 }, { 73, 99, 44, 2, 16 } };
        for (R = 0; R < 4; R++)
        {
            for (C = 0; C < 5; C++)

            {
                txtnumeros.Text += numeros[R, C] + "\t";
            }'
    
asked by Angel Ramos 24.11.2018 в 02:50
source

1 answer

0

The matrix begins with index 0, so for the human eye the 1 and 3 are the even rows. Keeping this in mind we create our guard:

for (R = 0; R < 4; R++)
{
   bool esPar = R == 1 || R == 3; // si la matriz no cambia el tamaño
   esPar = R + 1 % 2 == 0; // para cualquier tamaño
   //...

Then in the column loop we add the value of the columns

   //...
   for (C = 0; C < 5; C++)
   {
       if(esPar)
       {
           // asumimos result como int[] result = {0, 0 , 0, 0, 0}
           // declarado en el contexto de la variable numeros
           result[C] = result[C] + numeros[R, C]; '

In this way we save the value of the sum of the elements of each column of all the even rows in a result array.

Now, to calculate the largest element of the middle column you have to make a 'lock' to the middle column:

int indiceMedio = TotalColumnas / 2;

and in the loop of the columns do the operation, which does not correspond to do (not the previous exercise) because it is work for the student, they would have to talk to the teacher hehe.

But the idea of this exercise is to learn to intuitively traverse a matrix so here are some tips.

  • In an RxC matrix the orientation is determined by the nesting of the loops R-> C right to left, C-> R up and down and so on:
  • Operations with the current cell are performed in the body of the inner loop.
  • When we calculate averages or other operations of that type we must have access to the size of the matrix so it is better to save in a variable eg: int totalColumas = 5.
answered by 30.11.2018 в 23:38