You are on page 1of 2

Construccin de Software II Gua 18 Vectores y Matrices 106 JOptionPane.

.showMessageDialog(null," La suma de los elementos de la matriz es : "+suma+"\n La resta de los elementos de la matriz es : "+resta+"\n La multiplicacin de los elementos de la matriz es : "+multi+"\n La media aritmtica de los elementos de la matriz es : "+prom); for( i=0; i<f; i++){ //Lee la Matrix for( j=0; j<c; j++){ if(mat[i][j]>mayor){ mayor=mat[i][j]; } } } JOptionPane.showMessageDialog(null," El mayor valor de la matriz es "+mayor); menor=mayor; for( i=0; i<f; i++){ //Lee la Matrix for( j=0; j<c; j++){ if(mat[i][j]<menor){ menor=mat[i][j]; } } } JOptionPane.showMessageDialog(null," El menor valor de la matriz es "+menor); for( i=0; i<f; i++){ //Lee la Matrix for( j=0; j<c; j++){ if(mat[i][j]%2==0) { JOptionPane.showMessageDialog(null," El numero "+mat[i][j]+" es par y su posicin es "+i+","+j); cont++; } } } conti=(f*c)-cont; JOptionPane.showMessageDialog(null," La cantidad de numeros pares es : "+cont+"\n La cantidad de numeros impares es : "+conti ); } s=JOptionPane.showInputDialog(" DESEA SEGUIR TRABAJANDO CON VECTORES Y MATRICES? \n 1. SI \n 2. NO"); z=Integer.parseInt(s); } JOptionPane.showMessageDialog(null," :) GRACIAS POR VISITAR NUESTRO PROGRAMA \n TE ESPERAMOS EN LA PROXIMA :) "); JOptionPane.showMessageDialog(null," :) CHAO :) "); }//fin del main }//fin de la clase

Gua 18 Vectores y Matrices

Vectores y Matrices Introduccin Programa complejo de consola, en java, para tratar vectores y matrices. Prctica 105 import java.io.*; import javax.swing.JOptionPane; public class Vecmat { public static void main(String args[]) { String t=""; int mayor=0; int menor=0; double suma=0; int resta=0; int multi=1; boolean sw=false; int j=0; int z=1; int cont=0; int conti=0; JOptionPane.showMessageDialog(null," :) B i E n V e N i D o S :) "); JOptionPane.showMessageDialog(null," :) VAMOS A TRABAJAR CON VECTORES Y MATRICES :) "); JOptionPane.showMessageDialog(null," :) COMENCEMOS :) "); while(z==1){ t=""; int m; mayor=0; int k=0; String s; menor=0; suma=0; resta=0; multi=1; sw=false; int i; j=0; z=1; cont=0; conti=0; String op=JOptionPane.showInputDialog(" INGRESE 1 PARA CREAR VECTOR \n INGRESE 2 PARA CREAR UNA MATRIZ "); int ca=Integer.parseInt (op); switch (ca){ case 1: JOptionPane.showMessageDialog(null," ***HAS SELECCIONADO LA OPCIN 1, AHORA CREAREMOS UN VECTOR!!!*** "); String tam=JOptionPane.showInputDialog(" Ingrese el tamao deseado del vector :"); int n=Integer.parseInt (tam); int[] vec=new int[n];

Construccin de Software II 103

www.dariolara.com

www.dariolara.com

Construccin de Software II Gua 18 Vectores y Matrices 104 for(i=0;i<n;i++){ String dato=JOptionPane.showInputDialog(" Ingrese los elementos del vector : "); vec[i]=Integer.parseInt(dato); System.out.print(vec[i]+" "); } JOptionPane.showMessageDialog(null," VAMOS A CALCULAR LA SUMA, RESTA, MULTIPLICACIN Y PROMEDIO; \n LUEGO HALLAREMOS EL MAYOR Y EL MENOR NUMERO CON SUS RESPECTIVAS POSICIONES \n LOS PARES CON SU POSICIN Y EL TOTAL DE PARES E IMPARES \n BUSCAREMOS UN VALOR DENTRO DEL VECTOR "); for(i=0;i<n;i++){ suma=suma+vec[i]; resta=vec[i]-resta; multi=multi*vec[i]; } double prom=suma/n; JOptionPane.showMessageDialog(null," La suma de los elementos del vector es : "+suma+"\n La resta de los elementos del vector es : "+resta+"\n La multiplicacin de los elementos del vector es : "+multi+"\n La media aritmtica de los elementos del vector es : "+prom); for(i=0;i<n;i++){ if(vec[i]>mayor){ mayor=vec[i]; j=i; } } JOptionPane.showMessageDialog(null," El mayor valor del vector es "+mayor+" y esta en la posicion "+j); menor=mayor; for(i=0;i<n;i++){ if(vec[i]<menor){ menor=vec[i]; k=i; } } JOptionPane.showMessageDialog(null," El menor valor del vector es "+menor+" y esta en la posicion "+k); for(i=0;i<n;i++){ if(vec[i]%2==0){ JOptionPane.showMessageDialog(null," El numero "+vec[i]+" es par y esta en la posicin "+i); cont++; } } conti=n-cont; JOptionPane.showMessageDialog(null," La cantidad de numeros pares es : "+cont+"\n La cantidad de numeros impares es : "+conti ); String vb=JOptionPane.showInputDialog(" INGRESE EL ELEMENTO A BUSCAR EN EL VECTOR "); www.dariolara.com

Gua 18 Vectores y Matrices

int numb=Integer.parseInt(vb); for(i=0;i<n;i++){ if(numb==vec[i]){ n=i; sw=true; }else{ sw=false; } } if(sw==true){ JOptionPane.showMessageDialog(null," El numero "+numb+" si se encuentra dentro del vector y esta en la posicion "+n); }else{ JOptionPane.showMessageDialog(null," El numero "+numb+" no se encuentra dentro del vector "); } break; case 2: JOptionPane.showMessageDialog(null," ***HAS SELECCIONADO LA OPCIN 2, AHORA CREAREMOS UNA MATRIZ!!!*** "); String fil=JOptionPane.showInputDialog(" INGRESE NUMERO DE FILAS "); int f=Integer.parseInt (fil); String col=JOptionPane.showInputDialog(" INGRESE NUMERO DE COLUMNAS "); int c=Integer.parseInt (col); int mat[][] = new int[f][c]; for( i=0; i<f; i++){ //Lee la Matrix for( j=0; j<c; j++){ String num=JOptionPane.showInputDialog(" INGRESE LOS ELEMENTOS DE LA MATRIZ "); mat[i][j]=Integer.parseInt(num); } } for (i=0; i<f; i++){ //Escribe la matrix for(j=0; j<c; j++){ t=t+mat[i][j]+' '; } System.out.println(t); t=""; } JOptionPane.showMessageDialog(null," VAMOS A CALCULAR LA SUMA, RESTA, MULTIPLICACIN Y PROMEDIO; \n LUEGO HALLAREMOS EL MAYOR \n LOS PARES CON SU POSICIN Y EL TOTAL DE PARES E IMPARES "); for( i=0; i<f; i++){ //Lee la Matrix for( j=0; j<c; j++){ suma=suma+mat[i][j]; resta=mat[i][j]-resta; multi=multi*mat[i][j]; } } prom=suma/(f*c);

Construccin de Software II 105

www.dariolara.com

You might also like