Manejo Matrices



     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-5126364088863607"
     data-ad-slot="9520827110">



CREAR DOS MATRICES DE 5X5 Y LLENARLAS ALEATOREAMENTE, LUEGO RECORRER AMBOS ARREGLOS VERIFICANDO SI UN NUMERO DE REPITE EN AMBOS AREGLOS, PARA POSTERIORMENTE SER MOSTRADOS



import java.util.Random;
public class Ejemplo17
{
    public static void main(String[]args){
        int N = 5;
        int arreglo1 [][] = new int[N][N];
        int arreglo2 [][] = new int[N][N];
        int numerosRepetido [] = new int[N*N];
        int contador = 0;
     

        System.out.println("MATRIZ 1:");
        //RECORRE UNA MATRIZ DE 5X5
        for(int f=0;f<arreglo1.length;f++){
            for(int c=0; c<arreglo1.length;c++){
                arreglo1[f][c] = (int) (Math.random()*99+1);//LLENA LA MATRIZ CON NUMEROS ALEATORIOS
                System.out.print(arreglo1[f][c]+"\t");//IMPRIME MATRIZ CON FORMATO DE ESPACIO
            }
            System.out.println();//SALTO DE LINEA
        }
     
        System.out.println("Arreglo 2");
        //RECORRE OTRA MATRIZ DE 5X5
        for(int f=0;f<arreglo2.length;f++){
            for(int c=0; c<arreglo2.length;c++){
                arreglo2[f][c] = (int) (Math.random()*99+1);//LLENA MATRIZ CON NUMEROS ALEATORIOS
                System.out.print(arreglo2[f][c]+"\t");//IMPRIME MATRIZ CON FORMTO DE ESPACIO
            }
            System.out.println();//SALTO DE LINEA
        }
       
        for(int i=0; i<arreglo1.length;i++){//RECORRE LAS FILAS DE LA PRIMEA MATRIZ
            for(int j=0; j<arreglo1.length;j++){//RECORRE LAS COLUMNAS DE LA PRIMERA MATRIZ
                for(int f=0;f<arreglo2.length;f++){//RECORRE LAS FILAS DE LA SEGUNDA MATRIZ
                    for(int c=0; c<arreglo2.length;c++){//RECORRE COLUMNAS DE SEGUNDA MATRIZ
                     
                        if(arreglo1[i][j] == arreglo2[f][c]){ //SI EL DATO CONTENIDO EN LA POSIION [i][J] DE LA MATRIZ UNO
                                                              //ES IGUAL AL DATO CONTENIDO EN LA POSIION [f][c] DE A MATRIZ DOS
                            for(int k = contador;k>-1;k--){   //RECORRER ARREGLO QUE CONTIRNE LOS NUMEROS REPETIDOS
                                if(numerosRepetido[k]!= arreglo1[i][j]){//SI EL NUMERO REPETIDO NO ESTA EN EL ARREGLO                                  
                                    numerosRepetido[contador] = arreglo2[f][c];//AGREGA EL NUMERO AL ARREGLO                                
                                }                              
                            }
                            contador = contador +1;//AUMENTA TOPE DE ARREGLO
                        }
                     
                    }
                }
            }
        }
     
        for(int i = 0;i<numerosRepetido.length;i++){//RECORRE EL ARREGLO DE NUMEROS REPETIDOS
            if(numerosRepetido[i]!=0){//SI EN NUMERO E DIFERENTE DE 0
                System.out.println("Numeros repetidos: "+numerosRepetido[i]);//IMPRIMIR LOS NUMEROS QUE SE REPITEN
            }
        }
    }
}

Comentarios

Entradas más populares de este blog

Convertir decimal a binario, octal y hexadecima

Juego del gato