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

 

 


Tema destacado: Como proteger una cartera - billetera de Bitcoin


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Inicializar matriz?
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] 2 Ir Abajo Respuesta Imprimir
Autor Tema: Inicializar matriz?  (Leído 8,288 veces)
monsefoster

Desconectado Desconectado

Mensajes: 83



Ver Perfil
Inicializar matriz?
« en: 14 Noviembre 2009, 17:38 pm »

Hello chicos, estaba haciendo este programa y creo que me enrede...me podrian ayudar?

 :-\

Lo que quiero hacer es que el usuario introduzca el numero de filas y de columnas, y esta "dimensionarse" luego de eso...

Ej = filas: 3
columnas: 2
matriz[3][2];
y trabajar con eso despues de alli

tenia pensado algo asi?
Código:
BufferedReader en = new BufferedReader(new InputStreamReader (System.in));
int [][]m;
int tam1,tam2;
System.out.print ("Inserte cantidad de filas y columnas de la matriz: ");
System.out.flush();
tam1 = Integer.parseInt (en.readLine());
System.out.print ("Inserte cantidad de columnas de la matriz: ");
System.out.flush();
tam2 = Integer.parseInt (en.readLine());
m = new int [tam1][tam2];

ademas, quiero saber como hago para que cuando llene la matriz con numeros aleatorios, el "for" agarre las dimensiones correctas

pues en vectores es algo asi
Código:
public static void llenar(int m[]) {
int i;
for (i=0;i<m.length;i++){
m[i]=(int)((51-1)*Math.random()+10);
}

}
}
y yo habia pensado algo muuy parecido lo cual era:
Código:
public static void llenar(int m[][]) {
int j,i;
for (i=0;i<m.length;i++){
for (j=0;j<m.length;j++){
m[i][j]=(int)((51-1)*Math.random()+10);
}

}
}

Gracias  :huh:


En línea

Leyer


Desconectado Desconectado

Mensajes: 786


leyer@elhacker.net


Ver Perfil WWW
Re: Inicializar matriz?
« Respuesta #1 en: 14 Noviembre 2009, 17:46 pm »

utiliza el scanner en ves del BufferReader

Código
  1. Scanner scanner = new Scanner(System.in);

   
Código
  1. for(int  indexColumn=0; indexColumn<matriz.length; indexColumn++){
  2.                        //le colocas el numero de filas ejemplo: 2
  3. for(int indexRows=0;indexRows<2;indexRows++)
  4. matriz[ indexColumn][indexRows]= new Random().nextInt(10);
  5. }

Saludos y suerte


« Última modificación: 14 Noviembre 2009, 18:45 pm por L-EYER » En línea

monsefoster

Desconectado Desconectado

Mensajes: 83



Ver Perfil
Re: Inicializar matriz?
« Respuesta #2 en: 15 Noviembre 2009, 05:30 am »

Definitivamente estoy haciendo algo mal  :( :(

Código:
import java.io.*;
import java.util.*;
import funcionesm.*;
public class mainmain {

/**
* lee matriz
* saca el promedio
* ordena de menor a mayor
* ordena de mayor a menor
*
*
* @param args
*
*/
public static void main(String[] args) throws IOException {
Scanner en = new Scanner(System.in);
int [][]m;
int tam1;
System.out.print ("Inserte cantidad de filas y columnas de la matriz: ");
System.out.flush();
tam1 = Integer.parseInt (en.nextLine());
m = new int [tam1][tam1];
funciones.llenar (m);
funciones.promedio (m,tam1);
funciones.ordenar1 (m,tam1);
funciones.mostrar1 (m,tam1);



}
}

Código:
package funcionesm;
import java.io.*;
import java.util.*;

public class funciones {
/**
* Method llenar
*
*
*/
public static void llenar(int m[][],int tam1) {
int j,i;
for (i=0; i<m.length; i++){
for (j=0;j<m.length;j++){
m[i][j]= new Random().nextInt(10);
}

}
}

/**
* Method promedio
*
*
* @return
*
*/
public static double promedio(int m[][], int tam1) {
int i,c=0;
    int j,sum=0;
    double promedio;
    for (i=0;i<m.length;i++){
    for (j=0;j<tam1;j++){
    sum+=m[i][j];
    }
    }
    promedio = (sum/c);
   
    return promedio;
   
}

/**
* Method ordenar1
*
*
*/
public static void ordenar1(int m[][]) {
int x,i,aux,t;
for (i=0;i<m.length;i++){
for (x=0;x<m.length-1;x++){
for (t=0;t<m.length-1;t++){
if (m[x][t]>m[x+1][t+1]){
aux = m[x][t];
m[x][t]=m[x+1][t+1];
m[x+1][t+1]=aux;

}
}
}
}


}

/**
* Method mostrar1
*
*
*/
public static void mostrar1(int m[][],int tam1) {
int i,j;
for (i=0;i<m.length;i++){
for (j=0;j<tam1;j++){
System.out.println (+m[i][j]+ " ");
}
}
}
}

En línea

Leyer


Desconectado Desconectado

Mensajes: 786


leyer@elhacker.net


Ver Perfil WWW
Re: Inicializar matriz?
« Respuesta #3 en: 15 Noviembre 2009, 07:39 am »

que no te funciona?

-El motodo de ordenamiento de menor a mayor | mayor a menor?
-El promedio?

Saludos.
En línea

Leyer


Desconectado Desconectado

Mensajes: 786


leyer@elhacker.net


Ver Perfil WWW
Re: Inicializar matriz?
« Respuesta #4 en: 15 Noviembre 2009, 15:52 pm »

prueba este los ordena bien  menos a mayor y mayor a menor pero el promedio si no se si es asi.

Código
  1. import java.io.IOException;
  2. import java.util.Iterator;
  3. import java.util.Scanner;
  4. import java.util.SortedSet;
  5. import java.util.TreeSet;
  6.  
  7. class funciones {
  8. /**
  9. * Method llenar
  10. *
  11. */
  12. public static void llenar(int m[][],int nroRows) {
  13. int j,i,x=0;
  14. for (i=0; i<m.length; i++){
  15. for (j=0;j<nroRows;j++){
  16. m[i][j]= x;x++;
  17. }
  18. }
  19. }
  20. /**
  21. * Method promedio
  22. * @return
  23. */
  24. public static double promedio(int m[][], int tam1,int n) {
  25. int c=tam1;
  26.    int j,sum=0;
  27.    double promedio =0;
  28.     for (j=0;j<tam1;j++){
  29.     sum+=m[n][j];
  30.        promedio = sum/c;
  31.        return promedio;
  32.     }
  33. return promedio;
  34. }
  35. /**
  36. * Method ordenar1
  37. * @param b
  38. *
  39. *
  40. */
  41. public static void ordenar1(int m[][],int n, boolean b) {
  42. SortedSet<Integer> sortedSet = null;
  43. if(b){
  44. for(int index=0;index<m.length;index++){
  45. sortedSet = new TreeSet<Integer>();
  46. for(int j=0;j<n;j++){
  47. sortedSet.add(m[index][j]);
  48. }
  49. Iterator<Integer> s=sortedSet.iterator();
  50. int x=0;
  51. while(s.hasNext()){
  52. m[index][x]=s.next();
  53. x++;
  54. }sortedSet.clear();
  55.  
  56. }
  57. }else{
  58. for(int index=0;index<m.length;index++){
  59. sortedSet = new TreeSet<Integer>();
  60. for(int j=0;j<n;j++){
  61. sortedSet.add(m[index][j]);
  62. }
  63. Iterator<Integer> s = sortedSet.iterator();
  64. int ins[] = new int[n];
  65. int i=0;
  66. while(s.hasNext()){
  67. ins[i]= s.next();
  68. i++;
  69. }
  70. i=0;
  71. for(int y=n;y>0;y--){
  72. m[index][i]=ins[y-1];
  73. i++;
  74. }
  75.  
  76. }
  77. }
  78. }
  79.  
  80. /**
  81. * Method mostrar1
  82. *
  83. *
  84. */
  85. public static void mostrar1(int m[][],int tam1) {
  86. int i,j;
  87. for (i=0;i<m.length;i++){
  88. System.out.print("Columna: "+i+" Promedio= "+funciones.promedio(m, tam1, i)+" =  ");
  89.  
  90. for (j=0;j<tam1;j++){
  91. System.out.print (+m[i][j]+ ": ");
  92. }
  93. System.out.println("\n");
  94. }
  95. System.out.println("----------------------------");;
  96. }
  97. }
  98. public class mainmain {
  99.  
  100. /**
  101. * lee matriz
  102. * saca el promedio
  103. * ordena de menor a mayor
  104. * ordena de mayor a menor
  105. *
  106. *
  107. * @param args
  108. *
  109. */
  110. public static void main(String[] args) throws IOException {
  111. Scanner en = new Scanner(System.in);
  112. int [][]m;
  113.  
  114. int nroColumn =0;
  115. int nroRows   =0;
  116. System.out.println("----------------------------");
  117. System.out.print ("Inserte No. Columnas: ");
  118. nroColumn = en.nextInt();
  119. System.out.print ("Inserte Nro. Filas  : ");
  120. nroRows = en.nextInt();
  121. System.out.println("---------------------------");
  122. m = new int [nroColumn][nroRows];
  123. funciones.llenar (m,nroRows);
  124. // funciones.promedio (m,nroRows);
  125. System.out.println("Ordenar de Menor a Mayor S/N");
  126. String select = en.next();
  127. if(select.equals("S") || select.equals("s")){
  128. funciones.ordenar1 (m,nroRows,true);
  129. funciones.mostrar1(m, nroRows);
  130. }else{
  131. funciones.ordenar1 (m,nroRows,false);
  132. funciones.mostrar1 (m,nroRows);
  133. }
  134. }

En línea

monsefoster

Desconectado Desconectado

Mensajes: 83



Ver Perfil
Re: Inicializar matriz?
« Respuesta #5 en: 17 Noviembre 2009, 03:22 am »

Gracias!  ;D

Aunque, no se si estoy haciendo algo mal pero la matriz no me sale totalmente ordenada siempre queda algun elemento menor flotando por alli....  :-\

Quisiera saber cuantos metodos de ordenamiento hay?  :huh:

Gracias por la ayuda
En línea

Leyer


Desconectado Desconectado

Mensajes: 786


leyer@elhacker.net


Ver Perfil WWW
Re: Inicializar matriz?
« Respuesta #6 en: 17 Noviembre 2009, 03:35 am »

de menor a mayor y de mayor a menos aunke hay un problema no si insertas 2 number iguales no funcionara muy bien.  Saludos.
En línea

monsefoster

Desconectado Desconectado

Mensajes: 83



Ver Perfil
Re: Inicializar matriz?
« Respuesta #7 en: 18 Noviembre 2009, 02:49 am »

No hay numeros iguales...

Me refiero que hay metodo de burbuja, y otros...los cuales no conozco y quisiera buscar pero no se el nombre
En línea

1mpuls0


Desconectado Desconectado

Mensajes: 1.186


Ver Perfil
Re: Inicializar matriz?
« Respuesta #8 en: 18 Noviembre 2009, 05:16 am »

Quisiera saber cuantos metodos de ordenamiento hay?  :huh:

Hace algunos semestre lleve una materia que se llama Estructura de Datos y Miramos algunos metodos de Ordenacion

*Ordenación interna
-Algoritmos Ordenamiento por Intercambio
-Ordenacion Burbuja
-Quick Sort Ordenacion
-Shell Sort Ordenacion
-Algoritmos Ordenamiento Distribucion.
-Radix Ordenacion

*Ordenación Externa
-Algoritmos Ordenacion Externa
-Intercalacion Directa
-Mezcla Natural

La verdad no sé si son todos los que existan, pero me imagino qe los mas usados

Saludos
En línea

abc
monsefoster

Desconectado Desconectado

Mensajes: 83



Ver Perfil
Re: Inicializar matriz?
« Respuesta #9 en: 18 Noviembre 2009, 05:43 am »

Muchisimas Gracias!
En línea

Páginas: [1] 2 Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Inicializar HDD
Hardware
Polydeuces 5 12,009 Último mensaje 9 Octubre 2012, 03:04 am
por _Slash_
Dudita: Inicializar matriz desde función sin punteros, si es posible.
Programación C/C++
leosansan 2 2,015 Último mensaje 23 Diciembre 2013, 18:47 pm
por leosansan
Fallo en inicializar matriz
Java
BJM 1 1,555 Último mensaje 5 Junio 2014, 20:36 pm
por gordo23
Inicializar una matriz
Programación C/C++
JonaLamper 2 1,562 Último mensaje 7 Marzo 2016, 07:04 am
por furciorifa
como inicializar una matriz char**
Programación C/C++
sebapoli00 5 1,980 Último mensaje 3 Abril 2019, 17:44 pm
por K-YreX
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines