The thing is like this:
I have a array A[n]
, with variable length, entered by the user, which is then filled position by position with a value that the user also enters.
Now, what I did, is that even numbers that are within array A[n]
, will keep the other array B[contpares]
, which also has a variable length, which is assigned by counting the number of existing pairs terms in array A[n]
, to then assign these even values in the array B
, having a array
of only even numbers coming from A.
I also did the above described for odd numbers, and store them in array C[continpares]
.
My problem is when deploying the results:
Yes to the array A[n]
, I assigned the length, and then filled it with values only pairs, I correctly displayed the array B
, with the even terms of the array A
, and doing the same with odd values, I also have a correct display , but everything changes when I alternate odd and even values in Array A
, since it marks me error when displaying these values, and what I'm looking for is to display the 2 vectors B(pares de A) y C(inpares de A)
, at the same time , in this way:
assuming that A looks like this:
A={1,2,3,4}
array B
2,4
array c
1,3
I leave the code, here:
int val=0;
BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));
int A[];
int B[];
int C[];
int contpar=0;
int continpar=0;
int longv=0;
int temp1=0;
int temp2=0;
System.out.println("ingrese la longitud del vector A");
longv=Integer.parse.Int(br.readLine());
for(int i=0; i<longv; i++)
{
System.out.println("Ingrese valor para el vector A con posicion ["+(i+1)+"]");
val=Integer.parseInt(br.readLine());
A[i]=val;
}
for(int j=0; j<longv; j++)
{
if(A[j]%2==0)
{
contpar++;
}
if(A[j]%3==0)
{
continpar++;
}
}
B=new int [contpar];
C=new int[continpar];
for(int k=0; i<longv; k++)
{
if(A[K]%2==0)
{
temp1= A[k];
B[k]=temp1;
}
else
{
temp2= A[k];
C[k]=temp2;
}
}
for(int i=0; i<contpar; i++)
{
System.out.print(B[i]);
}
for(int j=0; j<continpar; j++);
{
System.out.print(C[j]);
}