Advertisement
urksiful

Arreglos Pre Final

Dec 8th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.11 KB | None | 0 0
  1. /*
  2.  * This code was develop by Urksiful
  3.  * I give a special thanks for my family
  4.  * And for my new cat Coso n.n
  5.  * So, here we go!!!
  6.  * "Always Remeber that programming is art, who knows program has the power to change the world"
  7.  * Linux Foundation
  8.  */
  9.  
  10. package finalproyect;
  11. import java.util.Random;
  12. import java.util.Scanner;
  13.  
  14. /**
  15.  *
  16.  * @author Uriel Cambrón Hernández
  17.  * @email ucambron@studiosegame.es.tl
  18.  * @blog urksiful.blogspot.mx
  19.  * @email urksiful@gmail.com
  20.  * @Copyright 2014  
  21.  */
  22.  
  23.  
  24. public class FinalProyect {
  25.     //Recuerda cambiar el nombre de la clase por la de tu proyecto
  26.     //Ejemplo: Sustituir "public class FinalProyect" por "public class NombredeTuClase"
  27.    
  28.    
  29.     /*En esta sección se creo el método estático para crear el arreglo*/
  30.     public static int[] creacionarreglo(int n){
  31.        Random rnd = new Random();        //Creación de la Clase Random
  32.        
  33.         //Declaración de variables
  34.         int a1[] = new int[n];  
  35.         //Rellenando el arreglo de n elementos mediante la variable rnd
  36.             for(int i=0;i<n;i++){
  37.                 //Aquí se usa la semilla "30" con la cuál se asiganaran valores de 0 a 30
  38.                 a1[i] = rnd.nextInt(30);
  39.             }
  40.     return a1;
  41.     }
  42.     /*En esta sección se desarrolla el contenido de los números pares e
  43.     impares. */
  44.     public static void pares(int a4[]){
  45.         int par=0,impar=0;
  46.         for(int i=0;i<a4.length;i++){
  47.             if(a4[i]%2==0){
  48.                 par++;
  49.             }else{
  50.             impar++;
  51.             }
  52.         }
  53.         System.out.println("En total hay "+par+" números pares y "+impar+" impares");
  54.        
  55.     }
  56.     /*En esta sección se desarrolla el contenido de los números pares e
  57.     impares. */
  58.     public static void imparreglo(int a3[]){
  59.         for(int i=0;i<a3.length;i++){
  60.             System.out.print(a3[i]+", ");
  61.         }
  62.             System.out.println();
  63.     }
  64.    
  65.    
  66.     public static void ordenascendente(int a[]){
  67.          //Creación del Arreglo Ascendente
  68.         int aux=0;
  69.             for (int i=0;i<a.length;i++){
  70.                 for (int j=0;j<(a.length-1);j++){
  71.                     if (a[j]>a[j+1]){
  72.                    
  73.                         aux=a[j];
  74.                         a[j]=a[j+1];
  75.                         a[j+1]=aux;
  76.                     }
  77.                 }
  78.             }      
  79.             System.out.println();
  80.             //Impresión Arreglo Ascendente  
  81.             System.out.println("Ordenamiento Ascendente ");
  82.               for (int i=0;i<a.length;i++){
  83.                   System.out.print(a[i]+" ");
  84.               }
  85.    
  86.     }
  87.    
  88.     public static void ordendescendente(int a1[]){
  89.         //Creación del Arreglo Descendente
  90.               int aux;
  91.               for (int i=0;i<a1.length;i++){
  92.                 for (int j=0;j<(a1.length-1);j++){
  93.                     if (a1[j+1]>a1[j]){
  94.                    
  95.                         aux=a1[j];
  96.                         a1[j]=a1[j+1];
  97.                         a1[j+1]=aux;
  98.                      }
  99.                 }
  100.               }
  101.             //Impresión  
  102.             //Impresión del arreglo descendente  
  103.             System.out.println();  
  104.             System.out.println("Arreglo descendente");
  105.               for (int i=0;i<a1.length;i++){
  106.                   System.out.print(a1[i]+" ");
  107.               }        
  108.    
  109.     }
  110.    
  111.     public static int[][] creacionmatriz(byte x, byte y){
  112.       Random rnd = new Random();
  113.      
  114.       int b[][]=new int[x][y];
  115.        
  116.         for(int i=0;i<x;i++){
  117.                for(int j=0;j<y;j++){
  118.                    b[i][j]=rnd.nextInt(100);
  119.                    if(b[i][j]>9){
  120.                        System.out.print(b[i][j]+"  ");}
  121.                    else{
  122.                        System.out.print(b[i][j]+"   ");
  123.                    }
  124.                }
  125.                System.out.println();
  126.                System.out.println();
  127.            }
  128.    
  129.     return b;
  130.     }
  131.    
  132.     public static void ordenmatrizfilas(int b1[][], int n, int m){
  133.         int aux;
  134.             for (int z=0;z<n;z++){    
  135.             for (int i=0;i<m;i++){
  136.                 for (int j=0;j<m-1;j++){
  137.                     if (b1[z][j]>b1[z][j+1]){
  138.                        
  139.                         aux=b1[z][j];
  140.                         b1[z][j]=b1[z][j+1];
  141.                         b1[z][j+1]=aux;
  142.                        
  143.                    
  144.                      }
  145.                 }
  146.                    
  147.               }
  148.            
  149.             }
  150.         for(int i=0;i<n;i++){
  151.                for(int j=0;j<m;j++){
  152.                    
  153.                    if(b1[i][j]>9){
  154.                        System.out.print(b1[i][j]+"  ");}
  155.                    else{
  156.                        System.out.print(b1[i][j]+"   ");
  157.                    }
  158.                }
  159.                System.out.println();
  160.                System.out.println();
  161.            }          
  162.    
  163.    
  164.     }
  165.    
  166.     public static void contadorfilas(int b[][],int n,int m){
  167.         int a[]=new int[n];
  168.         for (int i=0;i<n;i++){
  169.             for (int j=0;j<m;j++){
  170.                 a[i]+=b[i][j];
  171.            
  172.             }
  173.             System.out.println("La suma de elementos en la fila "+(i+1)+" es "+a[i]);    
  174.         }
  175.    
  176.     }
  177.    
  178.    
  179.        
  180.     public static void main(String[] args) {
  181.         Scanner leer = new Scanner(System.in);
  182.         byte opt, opt2, opt3;
  183.                  
  184.         System.out.println("Hola, este es el Proyecto Final de Programación");
  185.         System.out.println("Esta herramienta te ayudará al ordenamiento de ");
  186.         System.out.println("arreglos lineales y de matrices de nxm elementos");
  187.         System.out.println("Escoge una de las siguientes opciones: ");
  188.         System.out.println();
  189.                      do {
  190.                      System.out.println("1) Arreglos Unidimensionales");
  191.                      System.out.println("2) Arreglos Bidimensionales (Matrices)");
  192.                      System.out.println("0) Salir");
  193.                         opt=leer.nextByte();
  194.                      switch (opt){
  195.                          case 1:
  196.                              System.out.println("Ingresa el número de elementos deseados en el arreglo");
  197.                              int n=leer.nextInt();
  198.                              int a2[] = creacionarreglo(n);
  199.                              imparreglo(a2);
  200.                              System.out.println("Ahora selecciona la opción que te convenga");
  201.                              System.out.println("1) Ordenar el arreglo ascendentemente");
  202.                              System.out.println("2) Ordenar el arreglo descendentemente");
  203.                              opt2=leer.nextByte();
  204.                              if (opt2==2){ordendescendente(a2);}else{ordenascendente(a2);}
  205.                              System.out.println();
  206.                              pares(a2);
  207.                              System.out.println();
  208.                              System.out.println();
  209.                          
  210.                          case 2:
  211.                              System.out.println("Ingresa el número de filas deseado");
  212.                              Byte n1=leer.nextByte();
  213.                              System.out.println("Ahora ingresa el número de columnas");
  214.                              Byte m1=leer.nextByte();
  215.                              int b1[][]=creacionmatriz(n1,m1);
  216.                              System.out.println("Selecciona la opción deseada");
  217.                              System.out.println("1) Ordenar filas");
  218.                              System.out.println("2) Ordenar Columnas");
  219.                              opt3=leer.nextByte();
  220.                              if(opt3==1){
  221.                                  ordenmatrizfilas(b1,n1,m1);
  222.                                  contadorfilas(b1,n1,m1);
  223.                              }
  224.                      }
  225.                  
  226.                      }while(opt!=0);
  227.                
  228.               System.out.println("Gracias por usar esta herramienta");
  229.                  
  230.                
  231.             }
  232.            
  233.        
  234.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement