elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Tutorial básico de Quickjs


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  sumar filas y columnas en c#
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: sumar filas y columnas en c#  (Leído 9,029 veces)
eddymaltos13

Desconectado Desconectado

Mensajes: 1


Ver Perfil
sumar filas y columnas en c#
« 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


« Última modificación: 29 Octubre 2016, 02:51 am por engel lex » En línea

plizze4

Desconectado Desconectado

Mensajes: 2


Ver Perfil
Re: sumar filas y columnas en c#
« Respuesta #1 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.


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines