sumar filas y columnas en c#

(1/1)

eddymaltos13:
tengo que sumar los elementos de cada fila y cada columna de una matriz

esto es lo que llevo hasta el momento

     Código
      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

plizze4:
Probar inicializando el arreglo de esta manera

Código
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.

Navegación

[0] Índice de Mensajes