ejemplos de vectores y matrices - Materias de UES-FMO

Anuncio
package ejemplo1;
import javax.swing.JOptionPane;
/**
*
* @author joaquin
*/
public class Vectores2 {
public static int[] lectura(int n){
int[] vector=new int[n];
for(int i=0;i<n;i++)
vector[i]=Integer.parseInt(JOptionPane.showInputDialog("Ingrese dato ["+i+"] :"));
return vector;
}
public static int[] suma(int n, int a[], int b[]){
int[] vector=new int[n];
for(int i=0;i<n;i++)
vector[i]=a[i]+b[i];
return vector;
}
public static int[] resta(int n, int a[], int b[]){
int[] vector=new int[n];
for(int i=0;i<n;i++)
vector[i]=a[i]­b[i];
return vector;
}
public static int[] producto(int n, int a[], int b[]){
int[] vector=new int[n];
for(int i=0;i<n;i++)
vector[i]=a[i]*b[i];
return vector;
}
public static int mayor(int n, int a[]){
int valor;
valor=a[0];
for(int i=0;i<n;i++){
if(a[i]>valor){
valor=a[i];
}
}
return valor;
}
public static int menor(int n, int a[]){
int valor;
valor=a[0];
for(int i=0;i<n;i++){
if(valor>a[i]){
valor=a[i];
}
}
return valor;
}
public static void visualiza(int a[],String cadena, int n){
System.out.println("La operacion: "+cadena);
for(int i=0;i<n;i++)
System.out.println("Vector ["+i+"] :"+a[i]);
}
public static int[] ordenar(int n, int a[]){
int temp;
for(int i=0;i<n;i++){
for(int j=0;j<n­1;j++){
if(a[j]>a[j+1]){
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
return a;
}
public static void main(String[] args){
int m;
m=Integer.parseInt(JOptionPane.showInputDialog("Ingrese tamaño de vectores:"));
int[] vec_a=new int[m];
int[] vec_b=new int[m];
int[] suma_vec=new int[m];
int[] resta_vec=new int[m];
vec_a=lectura(m);
vec_b=lectura(m);
visualiza(suma(m,vec_a,vec_b),"Suma",m);
visualiza(resta(m,vec_a,vec_b),"Resta",m);
visualiza(producto(m,vec_a,vec_b),"Producto",m);
System.out.println("El elemento mayor es:"+mayor(m,vec_a));
System.out.println("El elemento menor es:"+menor(m,vec_a));
visualiza(ordenar(m,vec_a),"Ordenado",m);
}
}
package ejemplo1;
import javax.swing.JOptionPane;
/**
*
* @author joaquin
*/
public class Matrices {
public static int[][] leematriz(int m,int n){
int[][] matriz=new int[m][n];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
matriz[i][j]=Integer.parseInt(JOptionPane.showInputDialog("Ingrese dato ["+i+","+j+"]:"));
return matriz;
}
public static int[][] duplicamatriz(int m,int n, int mat[][]){
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
mat[i][j]=mat[i][j]*2;
return mat;
}
public static void visualizamat(int m, int n, int mat[][],String cadena){
System.out.println("La operacion realizada fue: "+cadena);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
System.out.print("E ["+i+","+j+"]:"+mat[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args){
int x,y;
x=Integer.parseInt(JOptionPane.showInputDialog("Ingrese dato m"));
y=Integer.parseInt(JOptionPane.showInputDialog("Ingrese dato n"));
int[][] mat=new int[x][y];
mat=leematriz(x,y);
visualizamat(x,y,duplicamatriz(x,y,mat),"Matriz Duplicada");
}
}
Descargar