1. Dadas dos matrices A y B intercambiar los minimos de A con los maximos de B
    import java.util.Scanner;
    
    public class JavaMatrices1 {
    
      Scanner Leer = new Scanner(System.in);
    
      void llenar (int M [] [], int f, int c)
      {
          
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
    		M [i] [j] = Leer.nextInt();
    	    }
    	}
        }
    
    
        void mostrar (int M [] [], int f, int c)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    System.out.println ();
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		System.out.print ("[" + M [i] [j] + "]");
    	    }
    	}
        }
    
    
        int menor (int M [] [], int f, int c)
        {
    	int men = M [1] [1];
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		if (M [i] [j] < men)
    		    men = M [i] [j];
    	    }
    	}
    	return (men);
        }
    
    
        int maximo (int M [] [], int f, int c)
        {
    	int max = M [1] [1];
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		if (M [i] [j] < max)
    		    max = M [i] [j];
    	    }
    	}
    	return (max);
        }
    
    
    
        void intercambiar (int A [] [], int fa, int ca, int B [] [], int fb, int cb)
        {
    	int min_a = menor (A, fa, ca);
    	int max_b = maximo (B, fb, cb);
    
    	//para cambiar los minimos de A con los maximos de B
    	for (int i = 1 ; i <= fa ; i++)
    	{
    	    for (int j = 1 ; j <= ca ; j++)
    	    {
    		if (A [i] [j] == min_a)
    		    A [i] [j] = max_b;
    	    }
    	}
    
    	//para intercambiar los maximos de con los minimos de A
    	for (int i = 1 ; i <= fb ; i++)
    	{
    	    for (int j = 1 ; j <= cb ; j++)
    	    {
    		if (B [i] [j] == max_b)
    		    B [i] [j] = min_a;
    	    }
    	}
        }
    
    
        public static void main (String args [])
        {
            Scanner Leer = new Scanner(System.in);
    	JavaMatrices1 h = new JavaMatrices1 ();
    	int A [] [] = new int [20] [20];
    	int B [] [] = new int [20] [20];
    	System.out.print ("Insert filas de A: ");
    	int fa = Leer.nextInt();
    	System.out.print ("Insert columnas de A: ");
    	int ca = Leer.nextInt();
    	System.out.print ("Insert filas de B: ");
    	int fb = Leer.nextInt();
    	System.out.print ("Insert columnas de B: ");
    	int cb = Leer.nextInt();
    
    	//lectura de matrices
    	System.out.println ("nINSERTANDO DATOS EN MATRIZ A: n");
    	h.llenar (A, fa, ca);
    	System.out.println ("nINSERTANDO DATOS EN MATRIZ B: n");
    	h.llenar (B, fb, cb);
    	System.out.println ("nMATRICES ORIGINALMENTE INSERTADAS: ");
    	h.mostrar (A, fa, ca);
    	System.out.println ();
    	h.mostrar (B, fb, cb);
    	System.out.println ();
    
    	//intercambiando elementos
    	h.intercambiar (A, fa, ca, B, fb, cb);
    	System.out.println ("nMATRICES DESPUES DEL INTERCAMBIO:");
    	h.mostrar (A, fa, ca);
    	System.out.println ();
    	h.mostrar (B, fb, cb);
        }
      
    }
    
  2. Dada una matriz cuadrada invertir su diagonal principal
    import java.util.Scanner;
    
    public class JavaMatrices2 {
    
      Scanner Leer = new Scanner(System.in);
    
      void llenar (int M [] [], int d)
        {
    	for (int i = 1 ; i <= d ; i++)
    	{
    	    for (int j = 1 ; j <= d ; j++)
    	    {
    		System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
    		M [i] [j] = Leer.nextInt();
    	    }
    	}
        }
    
    
        void mostrar (int M [] [], int d)
        {
    	for (int i = 1 ; i <= d ; i++)
    	{
    	    System.out.println ();
    	    for (int j = 1 ; j <= d ; j++)
    	    {
    		System.out.print ("[" + M [i] [j] + "]");
    	    }
    	}
        }
    
    
        void invierte (int M [] [], int d)
        {
    	int fin = d;
    	for (int i = 1 ; i <= d / 2 ; i++)
    	{
    	    int aux = M [i] [i];
    	    M [i] [i] = M [d] [d];
    	    M [d] [d] = aux;
    	    fin--;
    	}
        }
    
    
    
        public static void main (String args [])
        {
            Scanner Leer = new Scanner(System.in);
    	JavaMatrices2 h = new JavaMatrices2 ();
    	int M [] [] = new int [20] [20];
    	System.out.print ("Inserte dimen. de la matriz cuadrada: ");
    	int d = Leer.nextInt();
    	h.llenar (M, d);
    	System.out.print ("nMATRIZ ORIGINAL: ");
    	h.mostrar (M, d);
    	System.out.print ("nnMATRIZ CON LA DIAGONAL PRINCIPAL INVERTIDA: ");
    	h.invierte (M, d);
    	h.mostrar (M, d);
        }
      
        
    }
    
  3. Dada una matriz cuadrada invertir su diagonal secundaria
    import java.util.Scanner;
    
    public class JavaMatrices3 {
    
        Scanner Leer = new Scanner(System.in);
    
        void llenar (int M [] [], int d)
        {
    	for (int i = 1 ; i <= d ; i++)
    	{
    	    for (int j = 1 ; j <= d ; j++)
    	    {
    		System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
    		M [i] [j] = Leer.nextInt();
    	    }
    	}
        }
        void mostrar (int M [] [], int d)
        {
    	for (int i = 1 ; i <= d ; i++)
    	{
    	    System.out.println ();
    	    for (int j = 1 ; j <= d ; j++)
    	    {
    		System.out.print ("[" + M [i] [j] + "]");
    	    }
    	}
        }
        void invierte (int M [] [], int d)
        {
    	int fin = d;
    	for (int i = 1 ; i <= d / 2 ; i++)
    	{
    	    int aux = M [i] [d];
    	    M [i] [d] = M [d] [i];
    	    M [d] [i] = aux;
    	    fin--;
    	}
        }
        public static void main (String args [])
        {
            Scanner Leer = new Scanner(System.in);
    	JavaMatrices3 h = new JavaMatrices3 ();
    	int M [] [] = new int [20] [20];
    	System.out.print ("Inserte dimen. de la matriz cuadrada: ");
    	int d = Leer.nextInt();
    	h.llenar (M, d);
    	System.out.print ("nMATRIZ ORIGINAL: ");
    	h.mostrar (M, d);
    	System.out.print ("nnMATRIZ CON LA DIAGONAL SECUNDARIA INVERTIDA: ");
    	h.invierte (M, d);
    	h.mostrar (M, d);
        }
    }
    
  4. Dada dos matrices de diferentes tamaños R y S mostrar los elementos comunes de R en S
    import java.util.Scanner;
    
    public class JavaMatrices4 {
    
        Scanner Leer = new Scanner(System.in);
    
        void llenar (int M [] [], int f, int c)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
    		M [i] [j] = Leer.nextInt();
    	    }
    	}
        }
        void mostrar (int M [] [], int f, int c)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    System.out.println ();
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		System.out.print ("[" + M [i] [j] + "]");
    	    }
    	}
        }
        void comunes (int R [] [], int fr, int cr, int S [] [], int fs, int cs)
        {
    	System.out.print ("nnLos elementos comunes de R en S son: ");
    	for (int i = 1 ; i <= fr ; i++)
    	{
    	    for (int j = 1 ; j <= cr ; j++)
    	    {
    		for (int k = 1 ; k <= fs ; k++)
    		{
    		    for (int l = 1 ; l <= cs ; l++)
    		    {
    			if (R [i] [j] == S [k] [l])
    			    System.out.print ("[" + R [i] [j] + "]");
    		    }
    		}
    	    }
    	}
        }
        public static void main (String args [])
        {
            Scanner Leer = new Scanner(System.in);
    	JavaMatrices4 h = new JavaMatrices4 ();
    	int R [] [] = new int [20] [20];
    	int S [] [] = new int [20] [20];
    	System.out.print ("Inserte filas de R: ");
    	int fr = Leer.nextInt();
    	System.out.print ("Inserte columnas de R: ");
    	int cr = Leer.nextInt();
    	System.out.print ("Inserte filas de S: ");
    	int fs = Leer.nextInt();
    	System.out.print ("Inserte columnas de S: ");
    	int cs = Leer.nextInt();
    
    	System.out.print ("nLLENANDO MATRIZ R: n");
    	h.llenar (R, fr, cr);
    	System.out.print ("nLLENANDO MATRIZ S: n");
    	h.llenar (S, fs, cs);
    	System.out.print ("nLA MATRICES R : ");
    	h.mostrar (R, fr, cr);
    	System.out.print ("nLA MATRICES S : ");
    	h.mostrar (S, fs, cs);
    	h.comunes (R, fr, cr, S, fs, cs);
        }
    
        
    }
    
  5. Dada una matriz intercambiar los elementos de la primera columna con la ultima columna
    import java.util.Scanner;
    
    public class JavaMatrices5 {
    
        Scanner Leer = new Scanner(System.in);
    
        void llenar (int M [] [], int f, int c)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
    		M [i] [j] = Leer.nextInt();
    	    }
    	}
        }
        void mostrar (int M [] [], int f, int c)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    System.out.println ();
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		System.out.print ("[" + M [i] [j] + "]");
    	    }
    	}
        }
        void intercambiar (int M [] [], int f, int c)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    int aux = M [i] [1];
    	    M [i] [1] = M [i] 1;
    	    M [i] 1 = aux;
    	}
        }
        public static void main (String args [])
        {
            Scanner Leer = new Scanner(System.in);
    	JavaMatrices5 h = new JavaMatrices5 ();
    	int M [] [] = new int [20] [20];
    	System.out.print ("Inserte filas de la matriz: ");
    	int f = Leer.nextInt();
    	System.out.print ("Inserte columnas de la matriz: ");
    	int c = Leer.nextInt();
    
    	System.out.print ("nLLENANDO MATRIZ : n");
    	h.llenar (M, f, c);
    	System.out.print ("nLA MATRIZ ORIGINAL : ");
    	h.mostrar (M, f, c);
    	System.out.print ("nnLA MATRICES INTERCAMBIADA : ");
    	h.intercambiar (M, f, c);
    	h.mostrar (M, f, c);
        }
    }
    
  6. Contar el numero de digitos de cada elemento de una matriz
    import java.util.Scanner;
    
    public class JavaMatrices6 {
    
        Scanner Leer = new Scanner(System.in);
    
        void llenar (int M [] [], int f, int c)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
    		M [i] [j] = Leer.nextInt();
    	    }
    	}
        }
        void mostrar (int M [] [], int f, int c)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    System.out.println ();
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		System.out.print ("[" + M [i] [j] + "]");
    	    }
    	}
    
        }
        void cuenta (int M [] [], int f, int c)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		System.out.print ("n[" + M [i] [j] + "] tiene: " + digitos (M [i] [j]) + " digito(s)");
    	    }
    	}
        }
        int digitos (int n)
        {
    	int contador = 0;
    	while (n != 0)
    	{
    	    n = n / 10;
    	    contador++;
    	}
    	return (contador);
        }
        public static void main (String args [])
        {
            Scanner Leer = new Scanner(System.in);
    	JavaMatrices6 h = new JavaMatrices6 ();
    	int M [] [] = new int [20] [20];
    	System.out.print ("Inserte filas de la matriz: ");
    	int f = Leer.nextInt();
    	System.out.print ("Inserte columnas de la matriz: ");
    	int c = Leer.nextInt();
    
    	System.out.print ("nLLENANDO MATRIZ M: n");
    	h.llenar (M, f, c);
    	System.out.print ("nLA MATRIZ: ");
    	h.mostrar (M, f, c);
    	System.out.print ("nnCONTEO DE DIGITOS: ");
    	h.cuenta (M, f, c);
        }
    }
    
  7. Dada la matriz de m*n y el vector de tamaño n, determinar que columna de la matriz es igual al vector
    import java.util.Scanner;
    
    public class JavaMatrizVector1 {
    
        Scanner Leer = new Scanner(System.in);
    
        void llenarMatriz (int M [] [], int f, int c)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
    		M [i] [j] = Leer.nextInt();
    	    }
    	}
        }
        void mostrarMatriz (int M [] [], int f, int c)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    System.out.println ();
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		System.out.print ("[" + M [i] [j] + "]");
    	    }
    	}
        }
        void llenarVector (int V [], int d)
        {
    	for (int i = 1 ; i <= d ; i++)
    	{
    	    System.out.print ("Inserte pos.[" + i + "]: ");
    	    V [i] = Leer.nextInt();
    	}
        }
    
    
        void mostrarVector (int V [], int d)
        {
    	for (int i = 1 ; i <= d ; i++)
    	{
    	    System.out.print ("[" + V [i] + "]");
    	}
        }
        void procedure (int M [] [], int f, int c, int V [], int d)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    int sw = 1;
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    
    		for (int k = 1 ; k <= d ; k++)
    		{
    		    if (M [j] [i] != V [k])
    			sw = 0;
    		}
    	    }
    	    if (sw == 1)
    		System.out.println ("nnLa columna " + i + " es igual al vector");
    	}
        }
        public static void main (String args [])
        {
            Scanner Leer = new Scanner(System.in);
    	JavaMatrizVector1 h = new JavaMatrizVector1();
    	int M [] [] = new int [20] [20];
    	int V [] = new int [20];
    	System.out.print ("Inserte filas de la matriz: ");
    	int f = Leer.nextInt();
    	System.out.print ("Inserte dimension del vector: ");
    	int d = Leer.nextInt();
    
    	System.out.print ("nLLENANDO MATRIZ: n");
    	h.llenarMatriz(M, f, d);
    	System.out.print ("nLLENANDO EL VECTOR: n");
    	h.llenarVector (V, d);
    
    
    	System.out.print ("nLA MATRIZ: ");
    	h.mostrarMatriz(M, f, d);
    	System.out.print ("nnEL VECTOR: n");
    	h.mostrarVector (V, d);
    
    	h.procedure (M, f, d, V, d);
        }
        
    }
    
  8. Dada una matriz Z almacenar en un vector A la suma por sus columnas y en un vector B la suma por sus filas
    import java.util.Scanner;
    
    public class JavaMatrizVector2 {
    
        Scanner Leer = new Scanner(System.in);
    
        void llenarMatriz (int M [] [], int f, int c)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
    		M [i] [j] = Leer.nextInt();
    	    }
    	}
        }
        void mostrarMatriz (int M [] [], int f, int c)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    System.out.println ();
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		System.out.print ("[" + M [i] [j] + "]");
    	    }
    	}
        }
        void mostrarVector (int V [], int d)
        {
    	for (int i = 1 ; i <= d ; i++)
    	{
    	    System.out.print ("[" + V [i] + "]");
    	}
        }
        void vectorA (int M [] [], int f, int c, int A [], int d)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    int suma = 0;
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		suma = suma + M [j] [i];
    	    }
    	    A [i] = suma;
    	}
        }
        void vectorB (int M [] [], int f, int c, int B [], int d)
        {
    	for (int i = 1 ; i <= f ; i++)
    	{
    	    int suma = 0;
    	    for (int j = 1 ; j <= c ; j++)
    	    {
    		suma = suma + M [i] [j];
    	    }
    	    B [i] = suma;
    	}
        }
        public static void main (String args [])
        {
            Scanner Leer = new Scanner(System.in);
    	JavaMatrizVector2 h = new JavaMatrizVector2 ();
    	int Z [] [] = new int [20] [20];
    	int A [] = new int [20];
    	int B [] = new int [20];
    	System.out.print("Inserte filas de la matriz: ");
    	int f = Leer.nextInt();
    	System.out.print("Inserte columnas de la matriz: ");
    	int c = Leer.nextInt();
    
    	System.out.print("nLLENANDO MATRIZ: n");
    	h.llenarMatriz(Z, f, c);
    
    	System.out.print("nLA MATRIZ Z: ");
    	h.mostrarMatriz(Z, f, c);
    
    	System.out.println("nnSUMA POR COLUMNAS DE LA MATRIZ (vector A): ");
    	h.vectorA(Z, f, c, A, c);
    	h.mostrarVector (A, c);
    	System.out.println("nnSUMA POR FILAS DE LA MATRIZ (vector B): ");
    	h.vectorB(Z, f, c, B, f);
    	h.mostrarVector (B, f);
        }
    }
    

Categorias: J2SE, JAVA

2 Responses so far.

  1. Irwing Lule dice:

    Muy buen aporte para iniciar en el mundo de la programacion :) pero hasta donde pude observar en el primer ejercicio donde se halla el maximo esta lo sgte:
    if (M [i] [j] < max)
    max = M [i] [j];
    }

    y creo to que el simbolo debe ser mayor no menor :)

  2. Yami Rivera dice:

    En el ejercicio 5 tienes esto:
    void intercambiar (int M [] [], int f, int c)
    {
    for (int i = 1 ; i <= f ; i++)
    {
    int aux = M [i] [1];
    M [i] [1] = M [i] 1;
    M [i] 1 = aux;
    }
    Pero no hace nada, intercambia las columnas con lo siguiente
    void intercambiar (int M [] [], int f, int c)
    {
    for (int i = 1 ; i <= f ; i++)
    {
    int aux = M [i] [1];
    M [i] [1] = M [i] ;
    M [i] = aux;
    }
    }

Leave a Reply


Oracle 11g R2 Sesion

CONEXION USANDO CON ORACLE SQL DEVELOPER Abrimos el sql Developer […]

Conectar C# con SQL

Conectar C# con SQL Server desde una cadena de conexion. […]

Insertar datos en un

Creamos una base de datos en MySQL. Creamos una tabla […]

Creación de Bases d

Creamos una base de datos llamada “prueba” en MySQL. Creamos […]

Conectar PHP con MyS

Conectar PHP con MySQL usando la función mysqli de PHP. […]

No public Twitter messages.

Sponsors

  • Cheap reliable web hosting from WebHostingHub.com.
  • Domain name search and availability check by PCNames.com.
  • Website and logo design contests at DesignContest.com.
  • Reviews of the best cheap web hosting providers at WebHostingRating.com.