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

 

 


Tema destacado: Recopilación Tutoriales y Manuales Hacking, Seguridad, Privacidad, Hardware, etc


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  getPromedioColumnas(), algun tip ?
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: getPromedioColumnas(), algun tip ?  (Leído 1,531 veces)
rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
getPromedioColumnas(), algun tip ?
« en: 5 Diciembre 2016, 19:04 pm »

Ya puedo sacar el promedio de cada fila, pero de cada columna ? linea [75 a 85], tenia años sin hacer esto  :-X ,,,en la linea 38 llamo al metodo getPromedioFila y le paso un array, intente hacer algo parecido con la linea 51 de varias maneras pero me da java.lang.ArrayIndexOutOfBoundsException
Código
  1.  
  2. import java.text.DecimalFormat;
  3.  
  4. /*
  5.  * @Array 2D como promediar cada columna ?
  6.  */
  7. public class Array2Dimensiones implements Mostrar {
  8.  
  9. private int array[][];
  10. private int array2[][];
  11. private DecimalFormat dosDigi = new DecimalFormat("0.00");
  12.  
  13. public Array2Dimensiones( String descripcion, int array[][], int array2[][] ) {
  14. print("\""+descripcion+"\"\n");
  15. this.array = array;
  16. this.array2 = array2;
  17. mostrarArray();
  18. }
  19.  
  20. public void mostrarArray() {
  21. print("Array 2D completo\n");
  22. for( int tmp[] : array) {
  23. for( int tmp2 : tmp ) {
  24. print("{"+tmp2+"}");
  25. }
  26. print("\n");
  27. }
  28.  
  29. /*
  30. * PROMEDIO DE CADA FILA
  31. */
  32. print("Promedio de cada fila \n");
  33. for( int f=0; f<array.length; f++ ) {
  34.  
  35. for( int c=0; c<array.length; c++ ) {
  36. print(""+array[f][c]);
  37. }
  38. double promedio = getPromedioFila( array[f] );
  39. print("\t\t\t\t"+dosDigi.format(promedio)+"\n");
  40. }
  41.  
  42. /*
  43. * PROMEDIO DE CADA COLUMNA
  44. */
  45. print("Promedio de cada columna \n");
  46. for( int f=0; f<array.length; f++ ) {
  47. double promedio = 0;
  48.  
  49. for( int c=0; c<array[f].length; c++ ) {
  50. print(""+array[c][f]);
  51. promedio = getPromedioColumna(array[c]);
  52. }
  53.  
  54. print("\t\t\t\t"+dosDigi.format(promedio)+"\n");
  55. }
  56. }
  57.  
  58. /*
  59. * PROMEDIO FILA
  60. */
  61. public double getPromedioFila( int array[] ) {
  62.  
  63. int suma = 0;
  64. for( int f=0; f<array.length; f++ ) {
  65. suma += array[f];
  66. }
  67. return (double) suma / array.length;
  68.  
  69. }
  70.  
  71. /*
  72. * PROMEDIO COLUMNA
  73. */
  74. public double getPromedioColumna( int array[] ) {
  75.  
  76. int suma = 0;
  77. for( int f=0; f<array.length; f++ ) {
  78. for( int c=0; c<array[f]; c++ ) {
  79. suma += array[c];
  80. }
  81. }
  82. return (double) suma / array.length;
  83.  
  84. }
  85.  
  86. @Override
  87. public void print( String s ) { System.out.print(s); }
  88. public static void main( String []blablabl) {
  89.  
  90. int array[][] = {{1,2,3},
  91.                {4,5,6},
  92.                        {7,8,9}};
  93.  
  94. int array2[][] = {{1,2,3},
  95.         {4},
  96. {5,6,7}};
  97.  
  98. new Array2Dimensiones("2 tipos de Array 2d e Irregular",array,array2);
  99.  
  100. }
  101. }
  102.  

Código
  1. "2 tipos de Array 2d e Irregular"
  2. Array 2D completo
  3. {1}{2}{3}
  4. {4}{5}{6}
  5. {7}{8}{9}
  6. Promedio de cada fila
  7. 123 2,00
  8. 456 5,00
  9. 789 8,00
  10. Promedio de cada columna
  11. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
  12. at Array2Dimensiones.getPromedioColumna(Array2Dimensiones.java:80)
  13. at Array2Dimensiones.mostrarArray(Array2Dimensiones.java:52)
  14. at Array2Dimensiones.<init>(Array2Dimensiones.java:18)
  15. at Array2Dimensiones.main(Array2Dimensiones.java:99)
  16.  
  17.  


« Última modificación: 5 Diciembre 2016, 19:19 pm por solNegr0; » En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
kingcreek

Desconectado Desconectado

Mensajes: 16


Ver Perfil
Re: getPromedioColumnas(), algun tip ?
« Respuesta #1 en: 7 Diciembre 2016, 19:25 pm »

Te dejo lo que creo es la solucion si no entendi mal lo que quieres hacer, lo he ajustado para que sirva en array "regular" e "irregular".

Código
  1. import java.text.DecimalFormat;
  2.  
  3. /*
  4.  * @Array 2D como promediar cada columna ?
  5.  */
  6. public class Array2Dimensiones implements Mostrar {
  7.  
  8. private int array[][];
  9. private int array2[][];
  10. private DecimalFormat dosDigi = new DecimalFormat("0.00");
  11.  
  12. public Array2Dimensiones( String descripcion, int array[][], int array2[][] ) {
  13. print("\""+descripcion+"\"\n");
  14. this.array = array;
  15. this.array2 = array2;
  16. mostrarArray();
  17. }
  18.  
  19. public void mostrarArray() {
  20. print("Array 2D completo\n");
  21. for( int tmp[] : array) {
  22. for( int tmp2 : tmp ) {
  23. print("{"+tmp2+"}");
  24. }
  25. print("\n");
  26. }
  27.  
  28. /*
  29. * PROMEDIO DE CADA FILA
  30. */
  31. print("Promedio de cada fila \n");
  32. for( int f=0; f<array.length; f++ ) {
  33.    double promedio = 0;
  34.    int c;
  35. for( c=0; c<array[f].length; c++ ) {
  36. print(""+array[f][c]);
  37. promedio += array[f][c];
  38. }
  39. print("\t\t\t\t"+dosDigi.format(promedio / c)+"\n");
  40. }
  41.  
  42. /*
  43. * PROMEDIO DE CADA COLUMNA
  44. */
  45.  
  46. //obtenemos la longitud mas alta de las columnas para repetir el bucle for
  47. int cantidad = 0;
  48. int temp = 0;
  49. int x, y;
  50.        int[] segundo;
  51.        for (x = 0; x < array.length; ++x) {
  52.          segundo = array[x];
  53.          for (y = 0; y < segundo.length; ++y) {
  54.              temp++;
  55.          }
  56.          if(cantidad <= temp)
  57.          {
  58.              cantidad = temp;
  59.              temp = 0;
  60.          }
  61.        }
  62.  
  63. print("Promedio de cada columna \n");
  64.  
  65. for( int f=0; f<cantidad; f++ ) {
  66. double promedio = 0;
  67. int count = 0;
  68. int c;
  69. for( c=0; c < array.length; c++ ) {
  70.    //se filtran las posiciones del array con valor nulo
  71.    if(array[c].length > f)
  72.    {
  73.     print(""+array[c][f]);
  74.     promedio += array[c][f];
  75.     count++;
  76.    }
  77. }
  78. print("\t\t\t\t"+dosDigi.format(promedio / count)+"\n");
  79. }
  80.  
  81. }
  82.  
  83.  
  84. @Override
  85. public void print( String s ) { System.out.print(s); }
  86. public static void main( String []blablabl) {
  87.  
  88. int array[][] = {{1,2,3},
  89.                {4,5,6},
  90.                        {7,8,9}};
  91.  
  92. int array2[][] = {{1,2,3},
  93.         {4},
  94. {5,6,7}};
  95.  
  96. new Array2Dimensiones("2 tipos de Array 2d e Irregular",array,array2);
  97.  
  98. }
  99. }


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
algun problema......
Software
corazon inquieto 5 2,369 Último mensaje 31 Diciembre 2002, 10:55 am
por Armando
algun emulador.
Electrónica
cahotico 2 2,759 Último mensaje 3 Mayo 2004, 08:13 am
por who?
Algun MSN?
GNU/Linux
^DeMoN^ 7 3,397 Último mensaje 23 Noviembre 2010, 21:45 pm
por JuszR
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines