package practicamatrices;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class MATRICES {
public int n;
public int [][] x;
public MATRICES(int n){
this.n=n;
x = new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
x[i][j]=0;
}
}
}
public MATRICES(int [][] x){
this.x=x;
n= x.length;
}
public String toString(){
String texto="";
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
texto += "\t"+(int)Math.round(1000*x[i][j])/1000;
}
texto += "\n";
}
return texto;
}
public static int leer(BufferedReader buff){
int lee=0; boolean error;
do{
error = false;
try{
lee = Integer.parseInt(buff.readLine());}
catch (NumberFormatException ex){
System.out.println("SE ha producido un error");
error = true;
}
catch (Exception ex){ex.printStackTrace(System.err);}
}while(error);
return lee;
}
public static void cargar(int v[][]){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i=0; i<v.length; i++){
for( int j = 0; j < v.length; j ++ ){
System.out.print("Cargar vector: = ");
v[i][j]=leer(br);
}
}
}
public static void mostrar(int v[][]){
for(int i=0; i<v.length; i++){
for(int j=0;j< v.length; j++){
System.out.print("\t"+v[i][j]);
}
System.out.println();
}
}
public static double Diagonal(int [][] x){
double diagonal=0;
for(int i=0;i<x.length;i++){
diagonal = diagonal + x[i][i];
}
return diagonal;
}
public static int mayor( int V[ ][ ] ){
int mayor =0;
for( int i = 0; i < V.length; i ++ ){
for( int j = 0; j < V.length; j ++ ){
if(V[i][j] > mayor){
mayor = V[i][j];
}
}
}
return mayor;
}
public static int menor( int V[ ][ ]){
int menor = mayor(V);
for( int i = 0; i < V.length; i ++ ){
for( int j = 0; j < V.length; j ++ ){
if(V[i][j] < menor){
menor = V[i][j];
}
}
}
return menor;
}
public static void ordenar(int v[][]){
for(int i=0; i < v.length; i++){//ordena la matriz de abajo hacia arriba
for(int j=0;j< v.length; j++){
for(int x = 0; x < v.length; x++){
for(int y=0; y < v.length ; y++){
if(v[i][j] > v[x][y]){
int aux = v[i][j];
v[i][j] = v[x][y];
v[x][y] = aux;
}
}
}
}
}
}
public static void invertida(int v[][]){
for(int i=0; i < v.length; i++){//ordena la matriz de abajo hacia arriba
for(int j=0;j< v.length; j++){
for(int x = 0; x < v.length; x++){
for(int y=0; y < v.length ; y++){
if(v[i][j] < v[x][y]){
int aux = v[i][j];
v[i][j] = v[x][y];
v[x][y] = aux;
}
}
}
}
}
}
public static int Par(int v[][]){
int cantidad =0;
for(int i=0; i<v.length; i++){
for(int j=0;j< v.length; j++){
if(v[i][j] % 2 == 0)
cantidad++;
}
}
return cantidad;
}
public static int Impar(int v[][]){
int cantidad =0;
for(int i=0; i<v.length; i++){
for(int j=0;j< v.length; j++){
if(v[i][j] % 2 != 0)
cantidad++;
}
}
return cantidad;
}
public static double buscar(int V[][], int elem){
int pos =0;
for( int i = 0; i < V.length; i ++ ){
for(int j=0;j< V.length; j++)
if(V[ i ][j] == elem){
pos ++;
}
}
if( pos >= 1 ){
return 0;
}else
return -1;
}
public static int Igualdad(int v[][], int m[][]){
double pos=0 ;
for(int i=0; i<v.length; i++){
for(int j=0;j< v.length; j++)
if(v[i][j]==m[i][j]){
pos++;
}
}
if( pos == v.length*v.length ){
return 1;
}else
return 0;
}
public static void main(String [] args){
System.out.println("Tamaño de nuestra matriz Cuadrada: ");
Scanner e= new Scanner(System.in);
int n = e.nextInt();
int v[][] = new int[n][n];
int a[][] = new int[n][n];
int x[][] = new int[n][n];
System.out.println("Llenar matriz A");
cargar(v);
System.out.println();
System.out.println("Elemento a buscar en la matriz A: ");
Scanner f= new Scanner(System.in);
int elem = f.nextInt();
System.out.println("Llenar matriz B");
cargar(a);
System.out.println();
System.out.println("Cantidad de pares matriz A ="+ Par(v));
System.out.println("Cantidad de impares matriz A ="+ Impar(v));
System.out.println("El mayor matriz A es = "+ mayor(v));
System.out.println("El menor es matriz A = "+ menor(v));
System.out.println("La suma de la diagonal matriz A es = "+ Diagonal(v));
if(buscar(v,elem)==-1){
System.out.println("No se encentra el elemento.");
}else
System.out.println("Si se encuentra el elemento. ");
System.out.println();
System.out.println("Pares hay ="+ Par(a));
System.out.println("Impares hay ="+ Impar(a));
System.out.println("El mayor matriz es = "+ mayor(a));
System.out.println("El menor es matriz = "+ menor(a));
System.out.println("La suma de la diagonal es = "+ Diagonal(a));
System.out.println();
System.out.println("Matriz A Invertida ");
ordenar(v);
mostrar(v);
System.out.println();
System.out.println("Matriz A Ordednada");
invertida(v);
mostrar(v);
System.out.println();
System.out.println("Matriz B Invertida ");
ordenar(a);
mostrar(a);
System.out.println();
System.out.println("Matriz B Ordenada");
invertida(a);
mostrar(a);
if(Igualdad(v,a)==1){
System.out.println("si son iguales");
}else
System.out.println("no son iguales");
}
}
No hay comentarios:
Publicar un comentario