PRACTICA DE VECTORES Y MATRICES
PRACTICA
clace vector matriz
const const
tostring tostring
diagonal
completar
par\impares(contar)
mayor\ menor
ordenar
invertir
comparar (2 vect o 2 mat)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package vectorvisual1;
/**
*
* @author Romina
*/
public class Vector {
public int n;//Tam
public int [] x; //Arreglo
public Vector(int n){//SOBRECARGA DEL METODO
this.n = n;
x = new int[n];
for(int i = 0 ; i < n;i++){
x[i] = 0;
}
}
public Vector(int [] x){//SOBRECARGA DEL METODO
this.x=x;
n= x.length;
}
public String toString(){//REDEFINICION DE METODO
String texto="";
for(int i=0;i<n;i++){
texto += "\t"+(int)Math.round(1000*x[i])/1000;
}
texto += "\n";
return texto;
}
public int Mayor(int v [], int n){
int mayor = v.length;
for(int i = 0;i < n ; i++){
if(mayor < v[i]){
mayor = v[i];
}
}
return mayor;
}
public int menor(int v[], int n){
int menor = v.length;
for(int i = 0 ; i < n ; i++){
if(v[i] <= menor ){
menor = v[i];
}
}
return menor;
}
public int par(int v[], int n){
int cont = 0;
for(int i = 0; i < n; i++){
if(v[i] % 2 == 0){
cont = cont + 1;
}
}
return cont;
}
public int Impar(int v[], int n){
int cont = 0;
for(int i = 0; i < n; i++){
if(v[i] % 2 != 0){
cont = cont + 1;
}
}
return cont;
}
public void ordenar(int v[],int n){
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
if(v[i] < v[j]){
int aux = v.length;
aux = v[i];
v[i]= v[j];
v[j]= aux;
}
}
}
}
public void Invertir(int v[],int n){
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
if(v[i] > v[j]){
int aux = v.length;
aux = v[i];
v[i]= v[j];
v[j]= aux;
}
}
}
}
}
package matrizvisual;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
*
* @author Romina
*/
public class Matriz {
public int n;//Tam
public int [][] x; //Arreglo
public Matriz(int n){//SOBRECARGA DEL METODO
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 Matriz(int [][] x){//SOBRECARGA DEL METODO
this.x=x;
n= x.length;
}
public String toString(){//REDEFINICION DE METODO
String texto=" ";
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
texto += "\t "+(int)Math.round(1100*x[i][j])/1100;
}
texto += " \n";
}
texto += " \n";
return texto;
}
public static int Diagonal(int [][] x,int n){
int diagonal=0;
for(int i = 0 ; i < n ; i++){
diagonal = diagonal + x[i][i];
}
return diagonal;
}
public int par(int x[][], int n){
int cont = 0;
for(int i = 0; i < n; i++){
for(int j = 0 ; j < n ; j++){
if(x[i][j] % 2 == 0){
cont = cont + 1;
}
}
}
return cont;
}
public int Impar(int x[][], int n){
int cont = 0;
for(int i = 0; i < n; i++){
for(int j = 0 ; j < n ; j++){
if(x[i][j] % 2 != 0){
cont = cont + 1;
}
}
}
return cont;
}
public void ordenar(int x[][],int n){
for(int i = 0 ; i < n ; i++){//ordena la matriz de abajo hacia arriba
for(int j = 0 ; j < n ; j++){
for(int l = 0 ; l < n ; l++){
for(int m = 0 ; m < n ; m++){
if(x[i][j] < x[l][m]){
int aux = x[i][j];
x[i][j] = x[l][m];
x[l][m] = aux;
}
}
}
}
}
}
public void Ivertida(int x[][],int n){
for(int i = 0 ; i < n ; i++){//ordena la matriz de abajo hacia arriba
for(int j = 0 ; j < n ; j++){
for(int l = 0 ; l < n ; l++){
for(int m = 0 ; m < n ; m++){
if(x[i][j] > x[l][m]){
int aux = x[i][j];
x[i][j] = x[l][m];
x[l][m] = aux;
}
}
}
}
}
}
public int Mayor(int x[][], int n){
int mayor = x.length;
for(int i = 0;i < n ; i++){
for(int j = 0 ; j < n ; j++){
if(mayor < x[i][j]){
mayor = x[i][j];
}
}
}
return mayor;
}
public int menor(int x[][], int n){
int menor = x.length;
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
if(x[i][j] <= menor){
menor = x[i][j];
}
}
}
return menor;
}
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("Ingrese un valor: = ");
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.println(v[i][j]+"\n");
}
}
}
public static void main(String[] args) {
// TODO code application logic here
int P[][] = new int[5][5];
cargar(P);
mostrar(P);
}
}