You are on page 1of 37

DESARROLLO DE LA PRACTICA N1 EJERCICIO N1: Implementar una aplicacin que permita el ingreso de n valores numricos a un vector y se calcule: El promedio,

la desviacin estndar y la cantidad de elementos sobre el promedio. import java.io.*; class ejercicio1 { public static void main(String [ ] ar) { BufferedReader ingreso = new BufferedReader(new InputStreamReader(System.in)); int [] V ; int n=0; double prom=0, desviacion, comp; try { do { System.out.print("Ingrese La cantidad de elementos: "); n=Integer.parseInt(ingreso.readLine()); }while (n<0); V = new int [n]; llenarVector(V, n, ingreso); //se halla el promedio prom = promedio(prom, V, n); //se halla la desviacion estandar desviacion = desviacion(prom, V, n); //la cantidad de elementos mayores al promedio comp=comparacion(prom, V, n); System.out.println("El promedio de los numeros ingresados es: "+prom); System.out.println("La desviacion estandar es: "+desviacion); System.out.println("La cantidad de elementos mayores al promedio es: "+comp); } catch (Exception error) { System.out.println("Error en el ingreso de datos "+error); } } // definicion de funciones y procedimientos public static void llenarVector(int [] A, int x, BufferedReader ent) throws IOException { for(int i=0;i<x;i++)

{ System.out.print("El elemento n"+i+" del vector: "); A[i]= Integer.parseInt(ent.readLine()); } } public static double promedio(double prom, int [] V, int n) { for (int i=0;i<n;i++) { prom=prom+V[i]; } prom=prom/n; return prom; } public static double desviacion(double prom, int [] V, int n) { double desvcua, desv, desvestan=0; for (int i=0;i<n;i++) { desv=V[i]-prom; desvcua=desv*desv; for(i=0;i<n;i++) { desvestan=desvestan + desvcua; } } desvestan=Math.sqrt(desvestan/(n*(n-1))); return desvestan; } public static double comparacion(double prom, int [] V, int n) { int c=0; for (int i=0;i<n;i++) { if(V[i]>prom) { c++; } } return c; } }

EJERCICIO N2: Ingresar n valores enteros a un vector y determinar cuntos mltiplos de tres (03) y cuntos mltiplos de 7 existen en el vector.

import java.io.*; class ejercicio2 { public static void main (String [] ar) { BufferedReader ingreso = new BufferedReader(new InputStreamReader(System.in)); int [] V = new int [20]; int n=0, c7, c3; try { do { System.out.print("Ingresar la cantidad de elementos del vector: "); n= Integer.parseInt(ingreso.readLine()); } while(n<0); V=new int [n]; vector(V, n, ingreso); c3=mult3(V, n); c7=mult7(V, n); System.out.println("La cantidad de multiplos de tres es: "+c3); System.out.println("La cantidad de multiplos de siete es: "+c7); } catch(Exception error) { System.out.println("Error al ingresar datos: "); } } public static void vector(int [] V, int n, BufferedReader ent)throws IOException { for(int i=0;i<n;i++) { System.out.print("Ingresar el elemento n"+i+" del vector: "); V[i]=Integer.parseInt(ent.readLine()); } } public static int mult3(int [] V, int n) { int c=0; for(int i=0;i<n;i++) { if(V[i]%3==0) { c=c+1; }

} return c; } public static int mult7(int [] V, int n) { int c=0; for(int i=0;i<n;i++) { if(V[i]%7==0) { c=c+1; } } return c; } } EJERCICIO N3: Escriba un programa que permita el ingreso de n valores numricos en un vector y se determine: el menor, el mayor y el menor y las ubicaciones que ocupan, respectivamente, cada uno de ellos en el vector. import java.io.*; class ejercicio3 { public static void main (String [] ar) { BufferedReader ingreso = new BufferedReader(new InputStreamReader(System.in)); double [] V ; int n=0; double may, men; try { do { System.out.print("Ingresar la cantidad de elementos del vector: "); n= Integer.parseInt(ingreso.readLine()); } while(n<0); V = new double [n]; vector(V, n, ingreso); may=may(V, n); men=men(V, n); System.out.println("El numero mayor del vector es: "+may); System.out.println("El numero menor del vector es: "+men); System.out.println("El mayor numero ocupa el lugar: "+ubicmay(V, n, may)+" en el vector"); System.out.println("El menor numero ocupa el lugar: "+ubicmen(V, n, men)+" en el vector"); } catch(Exception error) {

System.out.println("Error al ingresar datos: "+error); } } public static void vector(double [] V, int n, BufferedReader ent)throws IOException { for(int i=0;i<n;i++) { System.out.print("Ingresar el elemento n"+i+" del vector: "); V[i]=Double.parseDouble(ent.readLine()); } } public static double may(double [] V, int n) { double may; may=V[0]; for(int i=0;i<n;i++) { if(may<V[i]) { may=V[i]; } } return may; } public static double men(double [] V, int n) { double men; men=V[0]; for(int i=0;i<n;i++) { if(men>V[i]) { men=V[i]; } } return men; } public static int ubicmay(double [] V, int n, double may) { int c=0; for(int i=0;i<n;i++) { if(may==V[i]) { c=i; } } return c; } public static int ubicmen(double [] V, int n, double men) { int c=0;

for(int i=0;i<n;i++) { if(men==V[i]) { c=i; } } return c; } } EJERCICIO N4: Se tiene en un vector las notas de n alumnos. Implementar un programa para calcular: el promedio de notas, la cantidad de aprobados, la cantidad de desaprobados, y cuantos tienen nota sobresaliente (notas entre 18 y 20, ambos inclusive). import java.io.*; class ejercicio4 { public static void main (String [] ar) { BufferedReader ingreso = new BufferedReader(new InputStreamReader(System.in)); double [] V ; int n=0, desaprobados, aprobados, destacados; double prom=0; try { do { System.out.print("Ingresar la cantidad de elementos del vector: "); n=Integer.parseInt(ingreso.readLine()); } }while(n<0); V = new double[n]; vector(V, n, ingreso); //se halla el promedio prom = promedio(prom, V, n); //se halla la cantidad de aprobados aprobados = aprobados(V, n); //la cantidad de desaprobados desaprobados=desaprobados(V, n); //La cantidad de destacados destacados=destacados(V, n); System.out.println("El promedio de los notas es: "+prom); System.out.println("La cantidad de aprobados es: "+aprobados); System.out.println("La cantidad de desaprobados es: "+desaprobados); System.out.println("La cantidad de destacados es: "+destacados); } catch(Exception error) {

System.out.println("Error en el ingreso de datos "+error); } } // definicion de funciones y procedimientos public static void vector(double [] V, int n, BufferedReader ent) throws IOException { for(int i=0;i<n;i++) { do { System.out.print("El elemento n"+i+" del vector: "); V[i]= Double.parseDouble(ent.readLine()); }while(V[i]<0||V[i]>20); } } public static double promedio(double prom, double [] V, int n) { for (int i=0;i<n;i++) { prom=prom+V[i]; } prom=prom/n; return prom; } public static int aprobados(double [] V, int n) { int c=0; for (int i=0;i<n;i++) { if(V[i]>=10.5) { c++; } } return c; } public static int desaprobados(double [] V, int n) { int c=0; for (int i=0;i<n;i++) { if(V[i]<10.5) { c++; } } return c; } public static int destacados(double [] V, int n) { int c=0; for (int i=0;i<n;i++)

{ if(V[i]>=18 && V[i]<=20) { c++; } } return c; } } EJERCICIO N5: Implementar un men de opciones con los siguientes mtodos de ordenamiento: Burbuja, insercin, seleccin, shell, quick sort. import java.io.*; class ejercicio5 { public static void main(String [ ] ar) { BufferedReader ingreso = new BufferedReader(new InputStreamReader(System.in)); double [] V; int n=0, opcion; // double prom=0, desviacion, comp; try { do { System.out.print("Ingrese La cantidad de elementos: "); n=Integer.parseInt(ingreso.readLine()); }while (n>20||n<1); V = new double [n]; llenarVector(V, n, ingreso); //Menu de ordenamiento System.out.println("Eliga la opcion(numero)del metodo que desea utilizar"); System.out.println("1. Metodo de la Burbuja(menor a mayor)"); System.out.println("2. Metodo de insercion"); System.out.println("3. Metodo de seleccion"); System.out.println("4. Metodo de Shell"); System.out.println("5. Metodo de quick sort"); System.out.print("La opcion es: "); opcion=Integer.parseInt(ingreso.readLine()); while(opcion>5||opcion<1) { System.out.print("La opcion ingresada no existe, ingrese nuevamente su opcion: "); opcion=Integer.parseInt(ingreso.readLine()); } switch (opcion) { case 1: burbuja(V, n); break;

case 2: insercion(V, n); break; case 3: seleccion(V, n); break; case 4: shell(V, n); break; case 5: quicksort(V, n); break; default: System.out.println("La opcion errada"); } for (int i = 0; i<n; i++) { System.out.println("El "+i+" elemento es: "+V[i]); } } catch (Exception error) { System.out.println("Error en el ingreso de datos "+error); } } // definicion de funciones y procedimientos public static void llenarVector(double [] V, int x, BufferedReader ent) throws IOException { for(int i=0;i<x;i++) { System.out.print("El elemento n"+i+" del vector: "); V[i]= Double.parseDouble(ent.readLine()); } } public static void burbuja(double [] V, int n) { double var; for (int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if (V[j]<V[i]) { var=V[i]; V[i]=V[j]; V[j]=var; } } } } public static void insercion(double [] V, int n) { double var; int primero, ultimo, c, k; for (int i=1;i<n;i++) { var=V[i];

primero=0; ultimo=i-1; //compara en base al lugar q ocupa while(primero<=ultimo) { c=(primero+ultimo)/2; if (var<V[c]) { ultimo=c-1; } else { primero=c+1; } } //asigna los valores al espacio respectivo for (k=i-1; k>=primero; k--) { V[k+1]=V[k]; } V[primero]=var; } } public static void seleccion(double [] V, int n) { double men, aux; int k; for (int i=0;i<n-1;i++) { men=V[i]; k=i; for(int j=i+1;j<n;j++) { if (men>V[j]) { men=V[j]; k=j; } } aux=V[i]; V[i]= men; V[k]= aux; } } public static void shell(double [] V, int n) { double aux; int d,i,sw; d=n; do{ d=d/2; do{

sw=0; i=-1; do{ i++; if(V[i]>V[i+d]) { aux=V[i]; V[i]=V[i+d]; V[i+d]=aux; sw=1; } }while(i+d!=n-1); }while(sw!=0); }while(d!=1); } public static void quicksort(double V[],int n) { int izq,der; double pivote, aux; izq=0; der=n-1; pivote=V[(n-1)/2]; while(izq<=der) { while(V[izq]<pivote && izq<n-1) { izq++; } while(V[der]>pivote) { der--; } if(izq<=der) { aux=V[izq]; V[izq]=V[der]; V[der]=aux; izq=izq+1; der=der-1; } } if(izq<n) { aux=V[der]; V[der]=V[0]; V[0]=aux; } else { aux=V[n-1]; V[n-1]=V[0]; V[0]=aux;

} } } EJERCICIO N6: Dado dos arreglos numricos A y B, generar un tercer arreglo C que sea producto de la intercalacin de A y B. import java.io.*; class ejercicio6 { public static void main(String [ ] ar) { BufferedReader ingreso = new BufferedReader(new InputStreamReader(System.in)); double [] V1; double [] V2; double [] C; int n=0, m=0, p=0; try { do { System.out.print("Ingrese la cantidad de elementos del primer vector: "); n=Integer.parseInt(ingreso.readLine()); }while (n<0); V1 = new double [n]; llenarVector1(V1, n, ingreso); do { System.out.print("Ingrese la cantidad de elementos del segundo vector: "); m=Integer.parseInt(ingreso.readLine()); }while (m<0); V2 = new double [m]; llenarVector2(V2, m, ingreso); p=m+n; C = new double [p]; burbuja(V1,n); burbuja(V2,m); intercalacion(V1,V2,C,n, m,p); for (int i = 0; i<p; i++) { System.out.println("El "+i+" elemento es: "+C[i]); } } catch (Exception error) { System.out.println("Error en el ingreso de datos "+error); } } // definicion de funciones y procedimientos public static void llenarVector1(double [] V1, int n, BufferedReader ent1) throws IOException

{ for(int i=0;i<n;i++) { System.out.print("El elemento n"+i+" del vector n1: "); V1[i]= Double.parseDouble(ent1.readLine()); } } public static void llenarVector2(double [] V2, int m, BufferedReader ent2) throws IOException { for(int i=0;i<m;i++) { System.out.print("El elemento n"+i+" del vector n2: "); V2[i]= Double.parseDouble(ent2.readLine()); } } public static void burbuja(double [] V, int x) { double var; for (int i=0;i<x;i++) { for(int j=i+1;j<x;j++) { if (V[j]<V[i]) { var=V[i]; V[i]=V[j]; V[j]=var; } } } } public static void intercalacion(double [] V1, double [] V2, double [] C, int n,int m, int p) { int i=0; int j=0; int k=0; while (i<n && j<m) { if(V1[i]<V2[j]) { C[k]=V1[i]; i++; } else { if(V1[i]==V2[j]) { C[k]=V1[i]; i++; k++; C[k]=V2[j]; j++;

} else { C[k]=V2[j]; j++; } }ccccc k++; } if(i>n-1) { for (int L = j; L<m; L++) { C[k]=V2[L]; k++; } } else { for (int L = i; L<n; L++) { C[k]=V1[L]; k++; } } } } EJERCICIO N7: Implementar las bsquedas secuencial y binaria en arreglos del tipo String. import java.io.*;

class ejercicio7 { public static void main(String [] args) { BufferedReader entrada = new BufferedReader (new InputStreamReader(System.in)); String [] Vec; // declaracion de variables int n=0, opcion=0, num=0; String valor=""; try

{ System.out.println("\tBusqueda secuencial y binaria\n"); System.out.print("Ingrese la cantidad de elementos del vector:"); do { n=Integer.parseInt(entrada.readLine()); }while(n<0); Vec=new String [n]; // Asignacion de Memoria para el Vector Vec llenarVector(Vec, n , entrada);

//Menu de busquedas System.out.println("Eliga la opcion(numero)del tipo de busqueda que desea utilizar"); System.out.println("1. Busqueda Secuencial"); System.out.println("2. Busqueda Binaria"); System.out.print("La opcion es: "); opcion=Integer.parseInt(entrada.readLine()); while(opcion>2||opcion<1) { System.out.println("La opcion ingresada no existe, ingrese nuevamente su opcion: "); opcion=Integer.parseInt(entrada.readLine()); } System.out.print("Ingrese el valor que desea buscar: "); valor=entrada.readLine(); switch (opcion) { case 1: num=busquedasecuencial(Vec, n, valor, num); if(num!=-1)

{ System.out.println("Fin de la busqueda"); System.out.println("El dato "+valor+" se encuentra en la posicion "+num); } else { System.out.println("El dato ingresado no hay en el vector"); } break; case 2: burbuja(Vec, n); System.out.println("El vector ordenado es: "); mostrarvector(Vec, n); num = busquedabinaria(Vec, n, valor, num); if(num!=-1) { System.out.println("Fin de la busqueda"); System.out.println("El dato "+valor+" se encuentra en la posicion "+num); } else { System.out.println("El dato ingresado no hay en el vector"); } break; default: System.out.println("La opcion errada"); }

} catch(Exception error) { System.out.println("error de datos"+error); } } public static void llenarVector(String []A, int x, BufferedReader ent)throws IOException { for (int i=0; i<x; i++) { System.out.print("El Elemento "+i+" es: "); A[i]=ent.readLine(); } } public static void mostrarvector(String [] Vec, int n) { for (int i = 0; i<n; i++) { System.out.println(Vec[i]+"\t"); } } public static int busquedasecuencial(String [] Vec,int n, String valor, int num) { for (int i = 0; i<n; i++) { if(Vec[i].equals(valor)) {

num=i; return num; } } return num; } public static int busquedabinaria(String [] Vec,int n, String valor, int num) { int izq=0, der=n-1; num=(izq+der)/2; while (Vec[num].compareTo(valor)!=0 && izq<=der) { if(valor.compareTo(Vec[num])>0) { izq=num+1; } else { der=num-1; } num=(izq+der)/2; } if(valor.compareTo(Vec[num])==0) { return num; } else

{ return -1; } } public static void burbuja(String [] V, int n) { String var; for (int i=0;i<n-1;i++) { for(int j=i+1;j<n;j++) { if (V[i].compareTo(V[j])>0) { var=V[i]; V[i]=V[j]; V[j]=var; } } } } } EJERCICIO N8: Ingresar desde teclado cualquier nmero entero y positivo y forme el mximo nmero posible que se pueda formar con los dgitos del nmero ingresado. Utilizar arreglos. import java.io.*; class ejercicio8 { public static void main(String [] args) { BufferedReader entrada = new BufferedReader (new InputStreamReader(System.in));

int [] Vec= new int [20]; // declaracion de variables int n=0, num=0; try { System.out.println("\tNUMERO MAXIMO DE UN NUMERO\n"); do { System.out.print("Ingrese el numero:"); num=Integer.parseInt(entrada.readLine()); }while(num<1); n=digitos(num,Vec,n); ordenar(Vec, n); System.out.print("El numero maximo es: "); mostrar(Vec, n); } catch(Exception error) { System.out.println("Error en el ingreso de datos:"+error); } } public static int digitos(int num, int [] Vec, int n) { while(num!=0) { Vec[n]=num % 10; n++; num=num/10; } return n; } public static void ordenar(int [] Vec, int n) { int may; for (int i = 0; i<n; i++) { for (int j = i+1; j<n; j++) { if(Vec[i]<Vec[j]) { may=Vec[i]; Vec[i]=Vec[j]; Vec[j]=may; } } } } public static void mostrar(int [] Vec, int n) { for (int i = 0; i<n; i++) {

System.out.print(Vec[i]); } System.out.println(); } }

EJERCICIO N9: Inicializar una matriz de n*m con el valor de una variable ingresada desde teclado. import java.io.*; class ejercicio9 { public static void main(String [] args) { BufferedReader entrada = new BufferedReader (new InputStreamReader(System.in)); int [][]M; int n=0, m=0; int valor=0; try { System.out.println("\tUNA MATRIZ N*M\n"); do { System.out.print("Cantidad de Filas:"); n=Integer.parseInt(entrada.readLine()); }while(n<1); do { System.out.print("Cantidad de Columnas: "); m=Integer.parseInt(entrada.readLine()); }while(m<1); M=new int [n][m]; System.out.print("Ingrese el valor para la matriz: "); valor=Integer.parseInt(entrada.readLine()); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { M[i][j]=valor; } } mostrarMatriz(M,n,m); } catch(Exception error) { System.out.println("Error en los datos:"+error); } } public static void mostrarMatriz(int [][]M, int f, int c)

{ for (int i = 0; i<f; i++) { for (int j = 0; j<c; j++) { System.out.print(M[i][j]+"\t"); } System.out.println(); } } } EJERCICIO N10:

Se tiene en una matriz el historial de ingresantes de las escuelas de la Facultad de Ingeniera desde el ao 1996 hasta el 2006 (semestres pares). Determinar: a. La cantidad de Ingresantes por ao de la FAI. b. La escuela con ms ingresantes. c. El ao con mayor nmero de ingresantes. d. El ao en que una escuela obtuvo ms ingresantes. import java.io.*; class ejercicio10 { public static void main(String [] args) { BufferedReader entrada = new BufferedReader (new InputStreamReader(System.in)); int [][] M; // declaracion de variables int m=0,num=0, school=0; String valor=""; try { System.out.println("\tHISTORIAL DE INGRESANTES\n"); do { System.out.print("El numero de escuelas:"); m=Integer.parseInt(entrada.readLine()); }while(m<0); M=new int [11][m]; // Asignacion de Memoria para la matriz llenarMatriz(M, m, entrada); System.out.println("La matriz ingresada es: "); mostrarMatriz(M, m); //a- La cantidad de ingresantes por ao de la FAI a(M, m); //b- la escuela con mayor numero de ingresantes b(M, m); //c- el ao con mayor numero de ingresantes c(M, m); //d- El ao que una escuela obtuvo mas ingresantes do {

System.out.println("Ingrese la escuela que desea conocer el ao con mayor numero de ingresantes es: "); school=Integer.parseInt(entrada.readLine()); }while(school>m||school<1); d(M, school); } catch(Exception error) { System.out.println("error de datos"+error); } } public static void llenarMatriz(int [][]M, int m, BufferedReader ent)throws IOException { for(int i=0;i<11;i++) { for(int j=0;j<m;j++) { System.out.print("La cantidad de ingresantes en el ao "+(1996+i)+" en la escuela "+(j+1)+": "); M[i][j]=Integer.parseInt(ent.readLine()); } } } public static void mostrarMatriz(int [][]M, int c) { for (int i = 0; i<11; i++) { for (int j = 0; j<c; j++) { System.out.print(M[i][j]+"\t"); } System.out.println(); } } public static void a(int [][]M, int m) { int sum=0, ao=0; for (int i = 0; i<11; i++) { sum=0; ao=i+1996; for (int j = 0; j<m; j++) { sum=sum+M[i][j]; } System.out.println("La cantidad de ingresantes en la FAI en el ao "+ao+" es: "+sum); } } public static void b(int [][]M, int m) { int may=0, sum=0, escuela=0;

for (int j = 0; j<m; j++) { sum=0; for (int i = 0; i<11; i++) { sum=sum+M[i][j]; } if(may<sum) { may=sum; escuela=j+1; } } System.out.println("La escuela con mayor numero de ingresantes es : "+escuela); } public static void c(int [][]M, int m) { int may=0, sum=0, ao=0; for (int i = 0; i<11; i++) { sum=0; for (int j = 0; j<m; j++) { sum=sum+M[i][j]; } if(may<sum) { may=sum; ao=i+1996; } } System.out.println("La mayor cantidad de ingresantes es en el ao: "+ao); } public static void d(int [][]M, int m) { int may=0, ao=0; m=m-1; for (int i = 0; i<11; i++) { if(may<M[i][m]) { may=M[i][m]; ao=1996+i; } } System.out.println("La escuela "+(m+1)+" en el ao "+ao+" tiene mayor numero de ingresantes("+may+")"); } }

EJERCICIO N11:

Ingresar desde teclado una lnea de texto, de a lo ms 25 letras, y almacenar cada una de las letras en una matriz de orden 5x5. Mostrar el texto y la Matriz. import java.io.*; class ejercicio11 { public static void main(String [] args) { BufferedReader entrada = new BufferedReader (new InputStreamReader(System.in)); char [][] M = new char [5][5]; String texto = new String(""); int n=0; try { System.out.println("\tMATRIZ DE UN TEXTO\n"); do { System.out.print("Ingrese el texto para la matriz: "); texto=entrada.readLine();//obtencion del texto n=texto.length(); }while(n>25); caracteres(M, texto, n); } catch(Exception error) { System.out.println("Error en los datos:"+error); } } public static void caracteres(char [][]M, String texto, int n) { char caracter; int p=0; for (int i = 0; i<5; i++) { for (int j = 0; j<5; j++) { if(p<n) { caracter=texto.charAt(p); System.out.print(caracter+"\t"); } else { System.out.print("\t"); } p++; } System.out.println();

} } }
EJERCICIO N12: Implementar programa que permita el ingreso desde teclado de dos matrices (M1 y M2) y muestre la matriz M3 resultante del producto de M1*M2.

import java.io.*; class ejercicio12 { public static void main(String [] args) { BufferedReader entrada = new BufferedReader (new InputStreamReader(System.in)); int [][]M1; int [][]M2; int [][]M3; int p=0, q=0, num=1, n=0, m=0; try { System.out.println("\tPRODUCTO DE MATRICES\n"); do { System.out.println("NO OLVIDAR QUE: el numero de columnas (de la 1 matriz)es el mismo el numero de filas (de la 2 matriz)"); System.out.println("La primera matriz"); do { System.out.print("Cantidad de Filas:"); p=Integer.parseInt(entrada.readLine()); }while(p<1); do { System.out.print("Cantidad de Columnas:"); q=Integer.parseInt(entrada.readLine()); }while(q<1); System.out.println("La segunda matriz"); do { System.out.print("Cantidad de Filas:"); n=Integer.parseInt(entrada.readLine()); }while(n<1); do { System.out.print("Cantidad de Columnas:"); m=Integer.parseInt(entrada.readLine()); }while(m<1); }while (q!=n); M1=new int [p][q]; llenarMatriz(M1,p,q,entrada,num); M2=new int [n][m];

num=2; llenarMatriz(M2,n,m,entrada, num); M3=new int [p][m]; producto(M1,M2,M3, p, m, q); System.out.println("La matriz producto es:"); mostrarMatriz(M3,p,m); } catch(Exception error) { System.out.println("Error en los datos:"+error); } } public static void llenarMatriz(int[][]Ma,int f,int c, BufferedReader ent, int num)throws IOException { System.out.println("Iniciaremos la matriz "+num); for(int i=0;i<f;i++) { for(int j=0;j<c;j++) { System.out.print("M["+i+"]["+j+"]:"); Ma[i][j]=Integer.parseInt(ent.readLine()); } } } public static void producto(int[][]M1, int[][]M2, int[][]M3, int p, int m, int q) { for(int i=0;i<p;i++) { for(int j=0;j<m;j++) { M3[i][j]=0; for (int t = 0; t<q; t++) { M3[i][j]=M3[i][j]+M1[i][t]*M2[t][j]; } } } } public static void mostrarMatriz(int [][]M3, int p, int m) { for (int i = 0; i<p; i++) { for (int j = 0; j<m; j++) { System.out.print(M3[i][j]+"\t"); } System.out.println(); } } } EJERCICIO N13:

Implementar una matriz para almacenar el horario de clases de un alumno. import java.io.*; class ejercicio13 { public static void main(String [] args) { BufferedReader entrada = new BufferedReader (new InputStreamReader(System.in)); String [][] M; // declaracion de variables int n=0; String valor=""; try { System.out.println("\t HORARIO DE CLASES \n"); do { System.out.print("Ingrese la cantidad de filas :"); n=Integer.parseInt(entrada.readLine()); }while(n<0); M=new String [n][7]; // Asignacion de Memoria para la matriz llenarMatriz(M, n, entrada); System.out.println("Tu horario es: "); mostrarMatriz(M, n); } catch(Exception error) { System.out.println("error de datos"+error); } } public static void llenarMatriz(String [][]M,int n, BufferedReader ent)throws IOException { for(int i=0;i<n;i++) { for(int j=0;j<7;j++) { System.out.print("Hora["+(i+1)+"],Dia["+j+"]:"); M[i][j]=ent.readLine(); } } } public static void mostrarMatriz(String [][]M, int f) { for (int i = 0; i<f; i++) { for (int j = 0; j<7; j++) { System.out.print(M[i][j]+"\t"); } System.out.println(); } }

}
EJERCICIO N14:

Hacer un programa para intercambiar filas y columnas de una matriz de n*m. import java.io.*; class ejercicio14 { public static void main(String [] args) { BufferedReader entrada = new BufferedReader (new InputStreamReader(System.in)); String [][]M1; int p=0, q=0, f1=0, f2=0, c1=0, c2=0; try { System.out.println("\tINTERCAMBIO DE FILAS Y COLUMNAS DE UNA MATRIZ\n"); do { System.out.print("Cantidad de Filas:"); p=Integer.parseInt(entrada.readLine()); }while(p<1); do { System.out.print("Cantidad de Columnas:"); q=Integer.parseInt(entrada.readLine()); }while(q<1); M1=new String [p][q]; llenarMatriz(M1,p,q,entrada); System.out.println("La matriz ingresada es:"); mostrarMatriz(M1,p,q); do { System.out.print("La 1 fila a intercambiar es:"); f1=Integer.parseInt(entrada.readLine()); }while (f1>=p || f1<0); do { System.out.print("La 2 fila a intercambiar es:"); f2=Integer.parseInt(entrada.readLine()); }while (f2>=p || f2<0); interfilas(M1,p, q, f1, f2); System.out.println("La matriz con filas intercambiadas es:"); mostrarMatriz(M1,p,q); do { System.out.print("La 1 columna a intercambiar es:"); c1=Integer.parseInt(entrada.readLine()); }while (c1>=q || c1<0); do {

System.out.print("La 2 columna a intercambiar es:"); c2=Integer.parseInt(entrada.readLine()); }while (c2>=q || c2<0); intercolumnas(M1,p, q, c1, c2); System.out.println("La matriz con columnas intercambiadas es:"); mostrarMatriz(M1,p,q); System.out.println("La matriz final es:"); mostrarMatriz(M1,p,q); } catch(Exception error) { System.out.println("Error en los datos:"+error); } } public static void llenarMatriz(String [][]Ma,int f,int c, BufferedReader ent)throws IOException { for(int i=0;i<f;i++) { for(int j=0;j<c;j++) { System.out.print("M["+i+"]["+j+"]:"); Ma[i][j]=ent.readLine(); } } } public static void interfilas(String [][]M1,int p, int q, int f1, int f2) { String aux=""; for(int i=0;i<q;i++) { aux=M1[f1][i]; M1[f1][i]=M1[f2][i]; M1[f2][i]=aux; } } public static void intercolumnas(String [][]M1,int p, int q, int c1, int c2) { String aux=""; for(int i=0;i<p;i++) { aux=M1[i][c1]; M1[i][c1]=M1[i][c2]; M1[i][c2]=aux; } } public static void mostrarMatriz(String [][]M1, int p, int q) { for (int i = 0; i<p; i++) { for (int j = 0; j<q; j++) {

System.out.print(M1[i][j]+"\t"); } System.out.println(); } } }
EJERCICIO N15:

Determinar si una matriz X es unitaria. import java.io.*; class ejercicio15 { public static void main(String [] args) { BufferedReader entrada = new BufferedReader (new InputStreamReader(System.in)); int [][]M; int n=0, m=0; boolean unitaria=false; try { System.out.println("\tUNA MATRIZ UNITARIA\n"); do { System.out.print("La cantidad de filas(es la misma que la de columnas): "); n=Integer.parseInt(entrada.readLine()); }while(n<1); m=n; M=new int [n][m]; llenarMatriz(M,n,m, entrada); System.out.println("La matriz ingresada es: "); mostrarMatriz(M,n,m); if(matrizuni(unitaria, M, n, m)) { System.out.println("La matriz es unitaria"); } else { System.out.println("La matriz no es unitaria"); } } catch(Exception error) { System.out.println("Error en los datos:"+error); } } public static void llenarMatriz(int [][]Ma,int f,int c, BufferedReader ent)throws IOException { for(int i=0;i<f;i++) { for(int j=0;j<c;j++)

{ System.out.print("M["+i+"]["+j+"]:"); Ma[i][j]=Integer.parseInt(ent.readLine()); } } } public static void mostrarMatriz(int [][]M, int f, int c) { for (int i = 0; i<f; i++) { for (int j = 0; j<c; j++) { System.out.print(M[i][j]+"\t"); } System.out.println(); } } public static boolean matrizuni(boolean unitaria, int [][]M, int n, int m) { for (int i = 0; i<n; i++) { for (int j = 0; j<m; j++) { if(i==j) { if(M[i][j]!=1) { return false; } } else if (M[i][j]!=0) { return false; } } } return true; } }
EJERCICIO N16:

Mostrar la diagonal principal de una matriz de orden n*n. import java.io.*; class ejercicio16 { public static void main(String [] args) { BufferedReader entrada = new BufferedReader (new InputStreamReader(System.in)); int [][]M;

int n=0; try { System.out.println("\TLA DIAGONAL DE UNA MATRIZ\n"); do { System.out.print("La cantidad de filas(es la misma que la de columnas): "); n=Integer.parseInt(entrada.readLine()); }while(n<1); M=new int [n][n]; llenarMatriz(M,n,entrada); System.out.println("La matriz ingresada es: "); mostrarMatriz(M,n); System.out.println("La diagonal de la matriz es: "); diagonal(M, n); } catch(Exception error) { System.out.println("Error en los datos:"+error); } } public static void llenarMatriz(int [][]Ma,int f,BufferedReader ent)throws IOException { for(int i=0;i<f;i++) { for(int j=0;j<f;j++) { System.out.print("M["+i+"]["+j+"]:"); Ma[i][j]=Integer.parseInt(ent.readLine()); } } } public static void mostrarMatriz(int [][]M, int f) { for (int i = 0; i<f; i++) { for (int j = 0; j<f; j++) { System.out.print(M[i][j]+"\t"); } System.out.println(); } } public static void diagonal(int [][]M, int n) { for (int i = 0; i<n; i++) { for (int j = 0; j<n; j++) { if(i==j) { System.out.print(M[i][j]+"\t");

} else { System.out.print("\t"); } } System.out.println(); } } }


EJERCICIO N17:

Generar automticamente la siguiente matriz: 0 1 1 1 1 1 0 1 1 1 x x 0 x x 2 2 2 0 2 2 2 2 2 0 import java.io.*; class ejercicio17 { public static void main(String [] arg) { BufferedReader ingreso =new BufferedReader( new InputStreamReader(System.in)); char [][] M =new char [5][5]; try { System.out.println("\t MATRIZ AUTOMATICA\n"); for (int i = 0; i<5; i++) { for (int j = 0; j<5; j++) { if(i==j) { M[i][j]='0'; } else { if(i==0||i==1) { M[i][j]='1'; } else { if(i==2) {

M[i][j]='x'; } else { M[i][j]='2'; } } } } } mostrarMatriz(M); } catch (Exception error) { System.out.print("Error al ingresar datos"+error); } } public static void mostrarMatriz(char [][]M) { for (int i = 0; i<5; i++) { for (int j = 0; j<5; j++) { System.out.print(M[i][j]+"\t"); } System.out.println(); } } }
EJERCICIO N18:

Implementar un programa que busque un nombre en una matriz del tipo String y muestre adems y fila y la columna en la que se encuentra (si se encuentra) el nombre buscado. import java.io.*; class ejercicio18 { public static void main(String [] args) { BufferedReader entrada = new BufferedReader (new InputStreamReader(System.in)); String [][] M; // declaracion de variables int n=0, m=0,num=0; String valor=""; try { System.out.println("\tBusqueda en una matriz\n"); do { System.out.print("Ingrese la cantidad de filas :"); n=Integer.parseInt(entrada.readLine()); }while(n<0);

do { System.out.print("Ingrese la cantidad de columnas :"); m=Integer.parseInt(entrada.readLine()); }while(m<0); M=new String [n][m]; // Asignacion de Memoria para la matriz llenarMatriz(M, n ,m, entrada); System.out.println("La matriz ingresada es: "); mostrarMatriz(M, n, m); System.out.print("Ingrese el valor que desea buscar: "); valor=entrada.readLine(); busquedasecuencial(M, n, m, valor); } catch(Exception error) { System.out.println("error de datos"+error); } } public static void llenarMatriz(String [][]M,int n,int m, BufferedReader ent)throws IOException { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print("M["+i+"]["+j+"]:"); M[i][j]=ent.readLine(); } } } public static void mostrarMatriz(String [][]M, int f, int c) { for (int i = 0; i<f; i++) { for (int j = 0; j<c; j++) { System.out.print(M[i][j]+"\t"); } System.out.println(); } } public static void busquedasecuencial(String [][]M,int n, int m, String valor) { boolean salir=false; System.out.println("entro1"); for (int i = 0; i<n; i++) { for (int j = 0; j<m; j++) { if(M[i][j].compareTo(valor)==0) { System.out.println("Fin de la busqueda");

System.out.println("El dato "+valor+" se encuentra en la fila "+i+" y la columna "+j); salir=true; } } } if(salir) { System.out.println("No se encuentra el dato"); } } }

You might also like