Free Web Site - Free Web Space and Site Hosting - Web Hosting - Internet Store and Ecommerce Solution Provider - High Speed Internet
Search the Web

Ejercicios ayudantía

1) Dado el arreglo : 20 - 30 - 40 - 0 ,insertar el elemento 10 para obtener el arreglo : 10 - 20 - 30 - 40


Ejemplo

Solución

import java.io.*;

class Invertir
{
    public static void main(String arg[]) throws IOException
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        
        System.out.print("Ingrese numero de elementos : ");
        
        int n = Integer.parseInt(in.readLine());
        
        int a[] = new int[n];
        
        /* lectura del arreglo por teclado */
        
        int i;
        
        for(i = 0; i < a.length; i++)
        {
            System.out.print("Ingrese elemento " + (i + 1) + " : ");
            a[i] = Integer.parseInt(in.readLine());    
        }    
        
        System.out.print("Arreglo ingresado : ");
        
        for(i = 0; i < a.length; i++)
            System.out.print(a[i] + "\t");
            
        System.out.print("\nIngrese elemento a insertar : ");
        int x = Integer.parseInt(in.readLine());    
        
        int tmp;
        i = 0;
        
        while(a[i] < x) i++;    /* se busca posicion i a insertar */
        
        while(i < a.length)
        {
            tmp = a[i];     /* se guarda lo que que hay en posicion i */
            a[i] = x;       /* se asigna el elemento a insertar x a la posicion i */
            x = tmp;        /* el nuevo elemento a insertar es tmp */        
            i++;            /* como x tiene que insertarse en la siguiente posicion se incrementa el indice */
        }
        
        System.out.print("Arreglo final : ");
        
        for(i = 0; i < a.length; i++)
            System.out.print (a[i] + "\t");
            
        System.out.println();    
    }    
}
Bajar archivo



2) Realizar un programa en java que intercambie los elementos desde "afuera hacia adentro"

Ej :  Arreglo inicial --> 4 - 2 - 7  - 12 - 23
      Arreglo final --> 23 - 12 - 7 -  2 - 4   

Algoritmo
Leer arreglo -> a[ ]
i = 0
j = a.length - 1
MIENTRAS( i < j ) HACER
    tmp = a[ i ]
    a[ i ] = a[ j ]
    a[ j ] = tmp
    i = i + 1
    j = j - 1
FIN MIENTRAS 


3) Implemetar un programa en JAVA que permita asignar coeficientes y
evaluar un polinomio de grado n del tipo : Px : a0 + a1x + a2x2 + a3x3 + ... + an-1xn-1 + anxn