Definir tres vectores del tamaño que el usuario indique.
static int[] v1,v2,v3,vecNuevo;
static int d;/* DIMENSION DE LOS VECTORES*/
public static void main(String arg[])
{
Scanner leer= new Scanner(System.in);
System.out.println("Qué dimension tendran los vectores? ");
d = leer.nextInt();
v1 = new int[d];
v2 = new int[d];
v3 = new int[d];
vecNuevo = new int[3];
crear dos vectores.
En uno se colocarán los carnet de los estudiantes que aprobaron Cálculo 1,
en otro los que aprobaron Etica.
Crear un tercer arreglo que contenga los carnet de los estudiantes que aprobaron
ambas materias..
static int[] vCalculo,vEtica,vComun;
public static void main(String arg[])
{
int nCal,nEt,comunes;
Scanner leer= new Scanner(System.in);
System.out.println("Cuantos alumnos aprobaron calculo 1? ");
nCal = leer.nextInt();
System.out.println("Cuantos alumnos aprobaron Etica 1? ");
nEt = leer.nextInt();
vCalculo = new int[nCal];
vEtica = new int[nEt];
System.out.println("Carnets de los alumnos que aprobaron calculo 1 ");
llenarVector(vCalculo, nCal);
System.out.println("Carnets de los alumnos que aprobaron Etica ");
llenarVector(vEtica, nEt);
comunes = (cuantosComunes(nCal,nEt));
vComun = new int[comunes];
llenarNuevo(nCal,nEt);
System.out.println("Carnets de los alumnos que aprobaron Etica y Calculo 1 ");
mostrarVector(vComun, comunes);
}
static void llenarNuevo(int d,int d2)
{
int pos =0;
for (int i=0; i < d; i++)
{
if (buscaNum(vCalculo[i],d2)== true)
{
vComun[pos] = vCalculo[i];
//meterElem(pos, vCalculo[i]);
pos++;
}
}
}
/* ME DICE CUANTOS CARNETS ESTAN EN AMBOS VECTORES. TIENE 2 PARAMETROS: d: ES LA DIMENSION
DE vCalculo, d2: DIMENSION DE vEtica.
BUSCA CADA ELEMENTO DE vCalculo en vEtica */
static int cuantosComunes(int d, int d2)
{
int acum=0;
for (int i = 0; i < d; i++)
{
if (buscaNum (vCalculo[i],d2) == true)
acum++;
}
return acum;
}
/* ESTE METODO ME INDICA SI UN NUMERO n ESTA EN EL VECTOR vEtica. ADEMAS DE n, TIENE UN
PARAMETRO d, QUE ES LA DIMENSION DE vEtica, NECESARIA PARA CONTROLAR EL FOR */
static boolean buscaNum(int n, int d)
{
for (int i=0; i<d; i++)
{
if (vEtica[i] == n)
return true;
}
return false;
}
/* METODO QUE ASIGNA VALORES A UN VECTOR DE DIMENSION D */
static void llenarVector(int v[],int d)
{Scanner leer= new Scanner(System.in);
for (int i=0; i < d; i++)
{
System.out.println("Introduzca el numero de la posicion " + i + " ");
v[i] = leer.nextInt();
}
}
/* ESTE METODO MUESTRA LOS ELEMENTOS DE UN VECTOR CUALQUIERA CON DIMENSION CUALQUIERA,
EL VECTOR ES EL PARAMETRO V Y LA DIMENSION DEL VECTOR X , SE PENSÓ ASI PARA PODER
UTILIZARLO EN EL VECTOR RESULTADO
*/
static void mostrarVector(int v[],int x)
{
for (int i=0; i < x; i++)
{
System.out.print(v[i] + " ");
}
}
public class Vector3 {
/*Se deben crear dos vectores uno de ellos de n elementos, el otro de m elementos .
Llenarlos con números ,colocándo en cada uno los elementos ordenados de menor a mayor.
Crear un tercer vector de tamaño n+m el cual contendrá los elementos de los vectores
ordenados de menor a mayor.*/
static int[] v1,v2,vResult;
public static void main(String arg[])
{
int n1,n2,n3;
Scanner leer= new Scanner(System.in);
System.out.println("Cuantos elementos tiene v1 ");
n1 =leer.nextInt();
System.out.println("Cuantos elementos tiene v2 ");
n2 = leer.nextInt();
n3 = n1 + n2;
v1 = new int[n1];
v2 = new int[n2];
vResult = new int [n3];
System.out.println("Llenando v1 ordenado");
llenarVector(v1, n1);
System.out.println("Llenando v2 ordenado");
llenarVector(v2, n2);
llenarNuevo(0, 0, 0, n1, n2);
System.out.println("El vector resultado es ");
mostrarVector(vResult, n3);
}
/* METODO RECURSIVO QUE ORDENA 2 VECTORES ORDENADOS DE MENOR A MAYOR EN UNO SOLO, DE MENOR
A MAYOR. RECIBE COMO PARAMETRS i: INDICE PARA V1, j: INDICE PARA V2, r:INDICE PARA
vResult, d1: DIMENSION DE V1, d2: DIMENSION DE V2 */
static void llenarNuevo(int i,int j, int r, int d1, int d2)
{
if ((i == d1) && (j == d2))
{
}
else if (i == d1)
{
vResult[r] = v2[j];
llenarNuevo(i, ++j, ++r, d1, d2);
}
else if (j == d2)
{
vResult[r] = v1[i];
llenarNuevo(++i, j, ++r, d1, d2);
}
else if (v1[i] <= v2[j])
{
vResult[r] = v1[i];
llenarNuevo(++i, j, ++r, d1, d2);
}
else
{
vResult[r] = v2[j];
llenarNuevo(i, ++j, ++r, d1, d2);
}
}
/* METODO QUE ASIGNA VALORES A UN VECTOR DE DIMENSION D */
static void llenarVector(int v[],int d)
{
for (int i=0; i < d; i++)
{Scanner leer= new Scanner(System.in);
System.out.println("Introduzca el numero de la posicion " + i + " ");
v[i] = leer.nextInt();
}
}
/* ESTE METODO MUESTRA LOS ELEMENTOS DE UN VECTOR CUALQUIERA CON DIMENSION CUALQUIERA,
EL VECTOR ES EL PARAMETRO V Y LA DIMENSION DEL VECTOR X , SE PENSÓ ASI PARA PODER
UTILIZARLO EN EL VECTOR RESULTADO
*/
static void mostrarVector(int v[],int x)
{
for (int i=0; i < x; i++)
{
System.out.print(v[i] + " ");
}
}
}
public class Vector4 {
static int[] v1 = new int[ 10 ];
public static void main( String[] args )
{
int[] v2, v3;
for( int i = 0; i < 10; i++ )
v1[ i ] = 1;
v2 = v1;
Imprime( v2 );
v3 = Suma1( v2 );
Imprime( v1 );
Imprime (v3);
}
static void Imprime( int[] elV )
{
for( int i = 0; i < 10; i++ )
System.out.print( elV[ i ] );
System.out.println();
}
static int[] Suma1( int[] elV )
{
for( int i = 0; i < 10; i++ )
elV[ i ]++;
return elV;
}
}
public class ordenar5 {
public static void main(String args[])
{Scanner leer= new Scanner(System.in);
int vec1[],vec2[],vecR[];
int n,m,nm;
System.out.print("Cuantos elementos tiene el vector 1? ");
n=leer.nextInt();
System.out.print("Cuantos elementos tiene el vector 2? ");
m=leer.nextInt();
nm=n+m;
// Dimensiono los vectores
vec1= new int[n];
vec2=new int[m];
vecR= new int[nm];
// LLeno los vectores 1 y 2
System.out.println("****Lectura Primer vector ordenado****");
llenarVector(vec1,n);
System.out.println("****Lectura Segundo vector ordenado****");
llenarVector(vec2,m);
// Crear vector resultante ordenado
vecR = ordenaVector(vec1,vec2,n,m);
// Mostrar vector resultante
System.out.println("**** vector resultante ordenado****");
mostrarVector(vecR,nm);
}
/**
* Metodo que permite llenar un vector conociendo
* su dimension
**/
static void llenarVector(int v[],int dim)
{
for (int i=0;i<dim;i++)
{Scanner leer= new Scanner(System.in);
System.out.print("v["+i+"]= ");
v[i]=leer.nextInt();
}
}
/**
* Metodo que crea un vector ordenado a
* partir de dos vectores ordenados recibidos
* como parametro
**/
static int[] ordenaVector(int v1[],int v2[],int dim1,int dim2)
{
int vr[];
vr=new int[dim1+dim2];
int i=0;// variable para controlar el indice del primer vector
int j=0;// variable para controlar el indice del segundo vector
int k=0;// variable para controlar el indice del vector resultante
while((i<dim1)&&(j<dim2))
{
if(v1[i]<v2[j])
{
vr[k]=v1[i];
k++;
i++;
}
else
{
if(v2[j]<v1[i])
{
vr[k]=v2[j];
k++;
j++;
}
else
{
vr[k]=v1[i];
k++;
i++;
j++;
}
}
}// fin del while
if (i==dim1)
{
for(int p=j;p<dim2;p++)
{
vr[k]=v2[p];
k++;
}
}
else
{
for(int p=i;p<dim1;p++)
{
vr[k]=v1[p];
k++;
}
}
return vr;
}
/**
* Metodo que muestra un vector dada su dimension
*
**/
static void mostrarVector(int v[],int dim)
{
for(int i=0;i<dim;i++)
{
System.out.println("v["+i+"]= "+v[i]);
}
}
}