| Ver tema anterior :: Ver siguiente tema | | Autor | Mensaje |
|---|
charly_rgn
Registrado: 25 Sep 2010 Mensajes: 2 Ubicación: lima - peru
| Publicado: 25/09/2010 11:26 pm | | | Título: ayuda para sumar matrices?? |
| saludos : cuando deseo sumar las filas y columnas tengo un inconveniente , no me suma la ultima columna ,alguna ayuda o otro metodo de hallar lo que solicito,,gracias
import java.io.*; class JavaMatrisVector3 { void llenarMatris (int M [] [], int f, int c)throws IOException { for (int i = 1 ; i <= f ; i++) { BufferedReader n = new BufferedReader(new InputStreamReader(System.in)); for (int j = 1 ; j <= c ; j++) { System.out.print ("Inserte pos[" + i + "][" + j + "]: "); M [i] [j] = Integer.parseInt(n.readLine()); } } }
void mostrarMatris (int M [] [], int f, int c) { for (int i = 1 ; i <= f ; i++) { System.out.println (); for (int j = 1 ; j <= c ; j++) { System.out.print ("[" + M [i] [j] + "]"); } } }
void mostrarVector (int V [], int d) { for (int i = 1 ; i <= d ; i++) { System.out.print ("[" + V [i] + "]"); } }
void vectorA (int M [] [], int f, int c, int A [], int d) { for (int i = 1 ; i <= f ; i++) { int suma = 0; for (int j = 1 ; j <= c ; j++) { suma = suma + M [j] [i]; } A [i] = suma; } }
void vectorB (int M [] [], int f, int c, int B [], int d) { for (int i = 1 ; i <= f ; i++) { int suma = 0; for (int j = 1 ; j <= c ; j++) { suma = suma + M [i] [j]; } B [i] = suma; } }
public static void main (String args [])throws IOException { JavaMatrisVector3 h = new JavaMatrisVector3 (); int Z [] [] = new int [20] [20]; int A [] = new int [20]; int B [] = new int [20];
BufferedReader fil = new BufferedReader(new InputStreamReader(System.in)); System.out.print ("Inserte filas de la matris: "); int f = Integer.parseInt(fil.readLine());
BufferedReader columna = new BufferedReader(new InputStreamReader(System.in)); System.out.print ("Inserte columnas de la matris: "); int c = Integer.parseInt(columna.readLine());
System.out.print ("\nLLENANDO MATRIS: \n"); h.llenarMatris (Z, f, c);
System.out.print ("\nLA MATRIZ Z: "); h.mostrarMatris (Z, f, c);
System.out.println ("\n\nSUMA POR COLUMNAS DE LA MATRIS (vector A): "); h.vectorA (Z, f, c, A, c); h.mostrarVector (A, c); System.out.println ("\n\nSUMA POR FILAS DE LA MATRIS (vector B): "); h.vectorB (Z, f, c, B, f); h.mostrarVector (B, f); } }
=================================== LA MATRIZ Z: [2][2][3] [3][3][3]
SUMA POR COLUMNAS DE LA MATRIS (vector A): [5][5][0] //mi problema
SUMA POR FILAS DE LA MATRIS (vector B): [7][9]GENERACIÓN CORRECTA (total time: 10 seconds) |
| | Volver arriba | |  | polly
Registrado: 19 Jul 2007 Mensajes: 618
| Publicado: 26/09/2010 7:11 am | | | Título: |
| Primero: deberias poner esto en la seccion de Java. Segundo: deberias utilizar el tag Code para publicar codigo fuente. Ej: | Código: | [code] codigo aqui [/code] |
Las funciones de vectorA y vectorB quedarian asi:
| Código: |
void vectorA (int M [] [], int f, int c, int A [], int d) { for (int j = 0 ; j < c ; j++) { int suma = 0; for (int i = 0 ; i < f ; i++) { suma = suma + M [i] [j]; } A [j] = suma; } } void vectorB (int M [] [], int f, int c, int B [], int d) { for (int i = 0 ; i < f ; i++) { int suma = 0; for (int j = 0 ; j < c ; j++) { suma = suma + M [i] [j]; } B [i] = suma; } }
|
PD: recorro el array desde el indice 0, asi que en el resto de tu programa deberias cambiar :
| Código: | for(int i = 1; i <= algo; i++) ...
|
por esto:
| Código: | for(int i = 0; i < algo; i++) ...
|
es mas estandar
S2 _________________ enrmarc |
| | Volver arriba | |  | jersonq
Registrado: 20 Oct 2010 Mensajes: 4 Ubicación: Colombia
| Publicado: 20/10/2010 7:02 am | | | Título: |
| Cómo se lo están indicando anteriormente, los vectores y matrices tienen un tamaño en cuanto a posiciones, pero las posiciones empiezan siempre desde cero, es asi que si tienes un vector v[] de 5 posiciones es decir v[5], la posicion del primer elemento en el vector es 0 y va hasta 4:
posicion: 0---1---2---3---4 vector[]: [3] [4] [7] [8 ] [2]
por eso el ciclo debe ser for(int x=0; x<5; x++) funciona igual para matrices en filas y columnas. |
| | Volver arriba | |  | | |
| No puede crear mensajes No puede responder temas No puede editar sus mensajes No puede borrar sus mensajes No puede votar en encuestas
|
|
| |