Problems with 3 arrasy A, B, and C

0

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]);
      }
    
asked by Game Generation 15.11.2018 в 16:58
source

1 answer

0

Basically what happens to you is that when you go through A to assign the values to the other 2 arrays, you try to put the values in the same positions of B and C as in the position that you read it from A, and therefore you generate an ArrayIndexOutofBounds.

It is arranged taking an index of the arrays B and C apart to know where you have to go putting them.

I've fixed the code a bit (but trying to respect you for the most part) and it would look like this:

        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.parseInt(br.readLine());
        A = new int[longv];
        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++;
            }else{
                continpar++;
            }

        }
        B=new int [contpar];
        C=new int[continpar];
        int indexB = 0;
        int indexC = 0;
        for(int k=0; k<longv; k++)
        {
            if(A[k]%2==0)
            {
                temp1= A[k];
                B[indexB]=temp1;
                indexB++;
            }
            else
            {
                temp2= A[k];
                C[indexC]=temp2;
                indexC++;
            }
        }

        for(int i=0; i<contpar; i++)
        {
            System.out.print(B[i]);
        }
        for(int j=0; j<continpar; j++)
        {
            System.out.print(C[j]);
        }  

Then on the other hand, in the condition that you use to count the odd and even pairs, I have simply set an else to the pair condition (otherwise it is odd) since you had a% 3 that is also not correct because for example the 6 meets that condition and it is clear that it is not odd.

    
answered by 15.11.2018 в 17:19