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?
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
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:
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:
utiliza el scanner en ves del BufferReader
Scanner scanner = new Scanner(System.in);
for(int indexColumn=0; indexColumn<matriz.length; indexColumn++){
//le colocas el numero de filas ejemplo: 2
for(int indexRows=0;indexRows<2;indexRows++)
matriz[ indexColumn][indexRows]= new Random().nextInt(10);
}
Saludos y suerte
Definitivamente estoy haciendo algo mal :( :(
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);
}
}
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]+ " ");
}
}
}
}
que no te funciona?
-El motodo de ordenamiento de menor a mayor | mayor a menor?
-El promedio?
Saludos.
prueba este los ordena bien menos a mayor y mayor a menor pero el promedio si no se si es asi.
import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;
class funciones {
/**
* Method llenar
*
*/
public static void llenar(int m[][],int nroRows) {
int j,i,x=0;
for (i=0; i<m.length; i++){
for (j=0;j<nroRows;j++){
m[i][j]= x;x++;
}
}
}
/**
* Method promedio
* @return
*/
public static double promedio(int m[][], int tam1,int n) {
int c=tam1;
int j,sum=0;
double promedio =0;
for (j=0;j<tam1;j++){
sum+=m[n][j];
promedio = sum/c;
return promedio;
}
return promedio;
}
/**
* Method ordenar1
* @param b
*
*
*/
public static void ordenar1(int m[][],int n, boolean b) {
SortedSet<Integer> sortedSet = null;
if(b){
for(int index=0;index<m.length;index++){
sortedSet = new TreeSet<Integer>();
for(int j=0;j<n;j++){
sortedSet.add(m[index][j]);
}
Iterator<Integer> s=sortedSet.iterator();
int x=0;
while(s.hasNext()){
m[index][x]=s.next();
x++;
}sortedSet.clear();
}
}else{
for(int index=0;index<m.length;index++){
sortedSet = new TreeSet<Integer>();
for(int j=0;j<n;j++){
sortedSet.add(m[index][j]);
}
Iterator<Integer> s = sortedSet.iterator();
int ins[] = new int[n];
int i=0;
while(s.hasNext()){
ins[i]= s.next();
i++;
}
i=0;
for(int y=n;y>0;y--){
m[index][i]=ins[y-1];
i++;
}
}
}
}
/**
* Method mostrar1
*
*
*/
public static void mostrar1(int m[][],int tam1) {
int i,j;
for (i=0;i<m.length;i++){
System.out.print("Columna: "+i+" Promedio= "+funciones.promedio(m, tam1, i)+" = ");
for (j=0;j<tam1;j++){
System.out.print (+m[i][j]+ ": ");
}
System.out.println("\n");
}
System.out.println("----------------------------");;
}
}
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 nroColumn =0;
int nroRows =0;
System.out.println("----------------------------");
System.out.print ("Inserte No. Columnas: ");
nroColumn = en.nextInt();
System.out.print ("Inserte Nro. Filas : ");
nroRows = en.nextInt();
System.out.println("---------------------------");
m = new int [nroColumn][nroRows];
funciones.llenar (m,nroRows);
// funciones.promedio (m,nroRows);
System.out.println("Ordenar de Menor a Mayor S/N");
String select = en.next();
if(select.equals("S") || select.equals("s")){
funciones.ordenar1 (m,nroRows,true);
funciones.mostrar1(m, nroRows);
}else{
funciones.ordenar1 (m,nroRows,false);
funciones.mostrar1 (m,nroRows);
}
}
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
de menor a mayor y de mayor a menos aunke hay un problema no si insertas 2 number iguales no funcionara muy bien. Saludos.
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
Cita de: monsefoster en 17 Noviembre 2009, 03:22 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
Muchisimas Gracias!
Aqui hay buena informacion
http://es.wikipedia.org/wiki/Algoritmo_de_ordenamiento