Advertisement
CodeWar

Metodo Matrices en java

Jan 4th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. //Ingresar 2 Matrices A y B intercambiar el valor minimo de A por el valor maximo de B
  2.     import java.util.Scanner;
  3.     class javaMatrices1
  4.     {
  5.         void llenar (int M [] [], int f, int c)
  6.         {
  7.             Scanner Leer=new Scanner(System.in);
  8.             for(int i=1;i<=f;i++)
  9.             {
  10.               for(int j=1;j<=c;j++)
  11.               {
  12.                   System.out.print("Inserte pos[" + i + "][" + j + "]: ");
  13.                   M [i][j] = Leer.nextInt();
  14.               }
  15.             }
  16.         }
  17.         void mostrar (int M [] [], int f, int c)
  18.         {
  19.             for(int i=1;i<=f;i++)
  20.             {
  21.                 System.out.println ();
  22.                 for(int j=1;j<=c;j++)
  23.                 {
  24.                     System.out.print("[" +  M [i] [j] + "]");
  25.                 }
  26.             }
  27.         }
  28.         int menor (int M [] [],int f, int c)
  29.         {
  30.             int men = M [1] [1];
  31.             for(int i=1;i<=f;i++)
  32.             {
  33.                 for(int j=1;j<=c;j++)
  34.                 {
  35.                     if(M [i] [j] < men)
  36.                         men = M [i] [j];
  37.                 }
  38.             }
  39.             return (men);
  40.         }
  41.         int maximo (int M [] [],int f, int c)
  42.         {
  43.             int max = M [1] [1];
  44.             for(int i=1;i<=f;i++)
  45.             {
  46.                 for(int j=1;j<=c;j++)
  47.                 {
  48.                     if(M [i] [j] > max)
  49.                         max = M [i] [j];
  50.                 }
  51.             }
  52.             return (max);
  53.         }
  54.         void intercambiar (int A [] [],int fa, int ca, int B [] [], int fb, int cb)
  55.         {
  56.             int min_a = menor (A, fa, ca);
  57.             int max_b = maximo (B, fb, cb);
  58.             //para cambiar el minimo de A con el maximo de B
  59.             for(int i=1;i<=fa;i++)
  60.             {
  61.                 for(int j=1;j<=ca;j++)
  62.                 {
  63.                     if(A [i] [j] == min_a)
  64.                         A [i] [j] = max_b;
  65.                 }
  66.             }
  67.             // para intercambiar el maximo B con el minimo de A
  68.             for(int i=1;i<=fb;i++)
  69.             {
  70.                 for(int j=1;j<=cb;j++)
  71.                 {
  72.                     if(B [i] [j] == max_b)
  73.                         B [i] [j] = min_a;
  74.                 }
  75.             }
  76.         }
  77.         public static void main (String args [])
  78.         {
  79.             javaMatrices1 h = new javaMatrices1 ();
  80.             Scanner Leer = new Scanner(System.in);
  81.             //declaramos las matrices
  82.             int A [] [] = new int [20] [20];
  83.             int B [] [] = new int [20] [20];
  84.            
  85.             //definimos el tamaño de las matrices
  86.             System.out.print ("Insert filas de A: ");
  87.             int fa = Leer.nextInt();
  88.              System.out.print ("Insert columnas de A: ");
  89.             int ca = Leer.nextInt();
  90.             System.out.print ("Insert filas de B: ");
  91.             int fb = Leer.nextInt();
  92.             System.out.print ("Insert columnas de B: ");
  93.             int cb = Leer.nextInt();
  94.            
  95.             //lectura de matrices
  96.             System.out.println ("\nINSERTANDO DATOS EN MATRIZ A: \n");
  97.             h.llenar(A, fa, ca);
  98.             System.out.println ("\nINSERTANDO DATOS EN MATRIZ B: \n");
  99.             h.llenar(B, fb, cb);
  100.             System.out.println ("\nMATRICES ORIGINALMENTE INSERTADOS: ");
  101.             h.mostrar(A, fa, ca);
  102.             System.out.println ();
  103.             h.mostrar(B, fb, cb);
  104.             System.out.println ();
  105.            
  106.             //intercambio elementos
  107.             h.intercambiar (A, fa, ca, B, fb, cb);
  108.             System.out.println("\nMATRICES DESPUES DEL INTERCAMBIO:");
  109.             h.mostrar(A, fa, ca);
  110.             System.out.println ();
  111.             h.mostrar(B, fb, cb);
  112.         }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement