Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: eddymaltos13 en 29 Octubre 2016, 02:48 am



Título: sumar filas y columnas en c#
Publicado por: eddymaltos13 en 29 Octubre 2016, 02:48 am
tengo que sumar los elementos de cada fila y cada columna de una matriz

esto es lo que llevo hasta el momento

    
Código
  1.       int M, N;
  2.            double suma = 0;
  3.  
  4.            Console.Write("filas: ");
  5.            N = int.Parse(Console.ReadLine());
  6.            Console.Write("columnas: ");
  7.            M = int.Parse(Console.ReadLine());
  8.  
  9.            int[][] matriz = new int[M][N]; <-------AQUI ME MARCA ERROR EN LA N
  10.  
  11.            for (int f = 0; f < matriz.Length; f++)
  12.            {
  13.                for (int c = 0; c < matriz[f].Length; c++)
  14.                {
  15.                    Console.Write("ingrese valores: ");
  16.                    matriz[f][c] = int.Parse(Console.ReadLine());
  17.                    suma = suma + matriz[f][c];
  18.                }
  19.            }
  20.            String martriz = "";
  21.            for (int f = 0; f < matriz.Length; f++)
  22.            {
  23.                for (int c = 0; c < matriz[f].Length; c++)
  24.                {
  25.                    martriz = martriz + matriz[f][c];
  26.                }
  27.                martriz = martriz + "\n";
  28.            }
  29.  
  30.            Console.Write(martriz);
  31.  
  32.            Console.Write("LA SUMA DE LA MATRIZ ES = " + suma);


Mod: Los códigos deben ir en etiquetas GeSHi, los temas va a su respectivo subforo, el titulo debe ser descriptivo


Título: Re: sumar filas y columnas en c#
Publicado por: plizze4 en 29 Octubre 2016, 04:44 am
Probar inicializando el arreglo de esta manera

Código
  1. int[][] matriz;
  2. matriz = new int[M][];
  3.  
  4. for(int y = 0; y < M ; y++)
  5. matriz[y] = new int[N];

igual te recomendaria que uses un arreglo rectangular.