Test Foro de elhacker.net SMF 2.1

Programación => Programación General => .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 (csharp) [Seleccionar]
       int M, N;
           double suma = 0;
           
           Console.Write("filas: ");
           N = int.Parse(Console.ReadLine());
           Console.Write("columnas: ");
           M = int.Parse(Console.ReadLine());

           int[][] matriz = new int[M][N]; <-------AQUI ME MARCA ERROR EN LA N

           for (int f = 0; f < matriz.Length; f++)
           {
               for (int c = 0; c < matriz[f].Length; c++)
               {
                   Console.Write("ingrese valores: ");
                   matriz[f][c] = int.Parse(Console.ReadLine());
                   suma = suma + matriz[f][c];
               }
           }
           String martriz = "";
           for (int f = 0; f < matriz.Length; f++)
           {
               for (int c = 0; c < matriz[f].Length; c++)
               {
                   martriz = martriz + matriz[f][c];
               }
               martriz = martriz + "\n";
           }

           Console.Write(martriz);

           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 (csharp) [Seleccionar]
int[][] matriz;
matriz = new int[M][];

for(int y = 0; y < M ; y++)
matriz[y] = new int[N];


igual te recomendaria que uses un arreglo rectangular.