Sie sind auf Seite 1von 13

Ao de la inversin para el desarrollo rural y

la Seguridad alimentaria












Escuela profesional: Ingeniera de sistemas

integrante: Guerrero Ramos Jorge


Curso: Tcnicas de programacin

Profesor: Ing. : Mrquez Valverde

Tema: Arreglos

Fecha: 20 de mayo del 2014

1. Programa para ingresar n nombres en un arreglo y luego reportarlos en orden
Alfabtico.

package tarea_sesion_4;
import java.util.Scanner;

public class ejercicio_01 {
private static Scanner leer = new Scanner(System.in);
static void tamao(){
System.out.println("Ingrese la cantidad de elemento que desea cargar:");
int numero = leer.nextInt();

String nombres[] = new String[numero + 1];
int tamao = nombres.length;

System.out.println("Ingrese "+numero+" nombres: ");
for(int i = 0; i < tamao ; ){
nombres[i] = leer.nextLine();
i++ ;
}
System.out.print("\nLos nombres ingresados son: ");
leer(nombres);

System.out.println("\n\nLos nombres despues de ordenar alfabeticamente son: ");
alfabeticamente(nombres);
}

static void leer( String [] n){
int tamao = n.length;
for (int i = 0 ; i < tamao ; i++ ){
System.out.println(n[i]);
}
}
static void alfabeticamente(String [] alf){
int tamao = alf.length;
for(int pase = 0 ; pase < tamao ; pase++ ){
for (int i = 0 ; i < tamao - 1 - pase ; i++ ){
if( alf[i].compareTo (alf[i + 1 ])> 0){
String actual;
actual=alf[i];
alf[i]=alf[i+1];
alf[i+1]=actual;
}
}
}
leer(alf);
}
public static void main(String[] args) {
tamao();
}
}









2. Programa para ingresar n nmeros en un arreglo y luego ingresar un nmero y
si este se encuentra eliminarlo en caso contrario reportar dato no se encuentra.
package tarea_sesion_4;
import java.io.*;

public class ejercicio_02 {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

double x[],num;
int n,i,p;
do{
System.out.print("Cantidad de elemento del arreglo:");
n=Integer.parseInt(br.readLine());
}while(n<=0);

x=new double[n];

for (i=0;i<n;i++)
{
System.out.print("x["+i+"]:");

x[i]=Double.parseDouble(br.readLine());
}

System.out.println("Datos Ingresados");
for(i=0;i<n;i++)
System.out.println("x["+i+"]:"+x[i]);
System.out.println("Numero a buscar:");
num=Double.parseDouble(br.readLine());
p=1;
for(i=0; i<n; i++)
if(x[i]==num)
{
p=i;
break;
}

if(p!=1)
{
for(i=p;i<n-1;i++)
x[i]=x[i+1];
n=n-1;
System.out.println("Nuevo Arreglo");
for(i=0;i<n;i++)
System.out.println("x["+i+"]"+x[i]);
}
else
System.out.println("El numero no se encuentra en el arreglo");
}
}









3. Ingresar los nombres y las notas de n alumnos y reportar la lista en orden
alfabtico y en orden de mrito
package tarea_sesion_4;
import java.io.*;
public class ejercicio_03 {
public static void main(String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String nombres[],tempNombre;
int n,i,j,notas[],tempNota;
do{
System.out.print("Numero de Alumnos:");
n=Integer.parseInt(br.readLine());
}while(n<=0);
nombres=new String[n];
notas=new int[n];
for(i=0;i<n;i++)
{
System.out.print("Nombres["+i+"]:");
nombres[i]=br.readLine();
System.out.print("Nota["+i+"]:");
notas[i]=Integer.parseInt(br.readLine());
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(nombres[i].compareToIgnoreCase(nombres[j])>0)
{
tempNombre=nombres[i];
nombres[i]=nombres[j];
nombres[j]=tempNombre;
tempNota=notas[i];
notas[i]=notas[j];
notas[j]=tempNota;
}
System.out.print("Lista en orden Alfabetico:");
for(i=0;i<n;i++)
System.out.println(nombres[i]+" "+notas[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(notas[i]<notas[j])
{
tempNombre=nombres[i];
nombres[i]=nombres[j];
nombres[j]=tempNombre;
tempNota=notas[i];
notas[i]=notas[j];
notas[j]=tempNota;
}
System.out.println("Lista en Orden Merito");
for(i=0;i<n;i++)
System.out.println(nombres[i]+" "+notas[i]);
}
}

Das könnte Ihnen auch gefallen