add rows, columns and diagnoales [closed]

-2

I must make a magic painting, I already have the matrix and I read the values that are in it, later I want to first add the rows, but I do not know how to do so that automatically positions 0.0,0,1,0,2 etc etc , I have the final part of the code so

Console.WriteLine("suma filas");
            for (i = 0; i < matriz.GetLength(0); i++)
            {
                for (j = 0; j < matriz.GetLength(1); j++)
                {
                    if (i>sumafilas)
                    {
                        sumafilas += matriz[i, i];
                    }


                }

                Console.Write(sumafilas);
                Console.WriteLine();
    
asked by jorge 03.08.2017 в 22:17
source

1 answer

1

To make the sums of a magic frame by cycles:

int [, ] mn = new int [3, 3] {{4,9,2}, {3,5,7}, {8,1,6}};

        //SUMA DE LINEAS
        for (int i = 0; i < mn.GetLength(0); i++)
        {
            int suma = 0;
            for (int j = 0; j < mn.GetLength(1); j++)
            {
                suma += mn[i, j];
            }
            Console.WriteLine("Linea " + i + " = " + suma);
        }
        //SUMA DE COLUMNAS

        for (int j = 0; j < mn.GetLength(0); j++)
        {
            int suma = 0;
            for (int i = 0; i < mn.GetLength(1); i++)
            {
                suma += mn[i, j];
            }
            Console.WriteLine("Columna " + j + " = " + suma);
        }
        //SUMA DE DIAGONAL \
        int sumad1 = 0;
        for (int i = 0; i < mn.GetLength(0); i++)
        {
            for (int j = i; j <= i; j++)
            {
                sumad1 += mn[i, j];
            }

        }
        Console.WriteLine("Linea diagonal \ = " + sumad1);
        //SUMA DE DIAGONAL /
        int sumad2 = 0;
        for (int j = 0; j < mn.GetLength(0); j++)
        {
            for (int i = mn.GetLength(0) -j - 1; i == mn.GetLength(0) -j - 1 ; i++)
            {
                sumad2 += mn[i, j];
            }

        }
        Console.WriteLine("Linea diagonal / = " + sumad2);

Greetings.

    
answered by 03.08.2017 / 23:09
source