Reverse plus Even and Odd of an array

I am a newbie to programming. I am trying to create a program that would display an array in reverse. Plus also find the even and odd numbers of an array,sum the count and also display the even and odd numbers. The code works but the problem is that it also reverses the even and odd arrays and it shows this weird zero in those arrays. What am I doing wrong? Please also provide explanation. Thanks!

 import java.util.Arrays;
 public class ArrayTest {

   public static void main(String[] args) 
  {
    int[] array = {1,2,3,4,5,6,7,8,9,10,11,12,13};
    for ( int i=0; i<array.length/2; i++ )
    {
        int temp = array[i];
        array[i] = array[array.length-(1+i)];
        array[array.length-(1+i)] = temp;
    }
    System.out.println("Array after reverse: \n" + Arrays.toString(array));

     int even=0;
     int odd=0;
     int[] Even = new int[13];
     int[] Odd = new int[13];
    for ( int i=0; i<array.length; i++)
    {
        if (array[i] % 2 == 0)
        {
            Even[i] = array[i];
            even++;
        }
        else
        {
            Odd[i] = array[i];
            odd++;
        }

    }
  System.out.println("Even: "+even+" ");
  System.out.println(Arrays.toString(Even));
  System.out.println("Odd:  "+odd+" ");
  System.out.println(Arrays.toString(Odd));

   }
}    

The output is:

Array after reverse:

[13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Even: 6

[0, 12, 0, 10, 0, 8, 0, 6, 0, 4, 0, 2, 0]

Odd: 7

[13, 0, 11, 0, 9, 0, 7, 0, 5, 0, 3, 0, 1]

3 answers

  • answered 2017-06-17 18:00 Ravi

    You need to correct your logic

     int[] Even = new int[(array.length/2)+1];
     int[] Odd = new int[(array.length/2)+1];
    for ( int i=0; i<array.length; i++)
    {
        if (array[i] % 2 == 0)
        {
            Even[even] = array[i];
            even++;
        }
        else
        {
            Odd[odd] = array[i];
            odd++;
        }
    
    }
    

    As per you code, you are initializing array of size 13 for odd and even, which is not correct.

     int[] Even = new int[13];
     int[] Odd = new int[13];
    

    So, by default, Even and Odd array will be initialized by 0 value. Then, you are setting value as per main array, which a size of 13 on alternate basis (even/odd).

    ==Updated==

    Since, you don't want Even and Odd array in reverse order. Then, you can move the code up.

    >>Demo<<

  • answered 2017-06-17 18:00 Matusevich Elena

    1. You select even and odd numbers from reversed array.
    2. You use wrong index for even and odd arrays.
    3. If you don't want to see zeros in output, use print in for statement. Another solution - firstly count odd and even numbers and create arrays with exact size.

      import java.util.Arrays; public class ArrayTest {

      public static void main(String[] args) {
      int[] array = {1,2,3,4,5,6,7,8,9,10,11,12,13};

      int even=0;
      int odd=0;
      int[] Even = new int[13];
      int[] Odd = new int[13];
      

      for ( int i=0; i

      } for ( int i=0; i

      System.out.println("Even: "+even+" "); System.out.println(Arrays.toString(Even)); System.out.println("Odd: "+odd+" "); System.out.println(Arrays.toString(Odd));

      } }

  • answered 2017-06-17 18:00 madhan_madhav

    You faced 2 problems (I guess so)

    1. The odd and even arrays are also in reverse order

      Reason: The first For loop reverses the 'array' and stores the results in array itself. So, the next time when you try working with 'array' to find odd/even numbers, you are actually working with the reversed array.

      Solution: You can assign the original array to a backup array and use that backup array to find odd and even nos.

    2. Unnecessary zeros:

      Reason: In your second for loop you used odd[i]=array[i] which seems to be a logical error in your code. Consider the case:

      value of i : 0 1 2 3 4 5 ... 12

      value of array[i]: 1 2 3 4 5 6 ... 13

      value of odd[i] : 1 0 3 0 5 0 ... 13

      value of even[i] : 0 2 0 4 0 6 ... 0

    This means, the control inside for loop is made to flow either to if{} block or the else{} block and not the both. So, when if(condition) is satisfied, then even[i] array will be updated. But meanwhile what happens to the odd[i] array? It retains the inital value '0'. That's it!

    I hope the following code helps you:

    import java.util.Arrays;
    public class A
    {
      public static void main(String[] args) 
      {
       int[] array = {1,2,3,4,5,6,7,8,9,10,11,12,13};
       int[] arr2 = new int[array.length];   // backup array
       arr2=Arrays.copyOfRange(array,0,array.length);
       for ( int i=0; i<arr2.length/2; i++ )
       {
        int temp = arr2[i];
        arr2[i] = arr2[arr2.length-(1+i)];
        arr2[arr2.length-(1+i)] = temp;
       }
       System.out.println("Array after reverse: \n" + Arrays.toString(arr2));
       int even=0;
       int odd=0;
       int[] Even = new int[13];
       int[] Odd = new int[13];
       for ( int i=0; i<array.length; i++)
       {
        if (array[i] % 2 == 0)
        {
            Even[even] = array[i];
            even++;
        }
        else
        {
            Odd[odd] = array[i];
            odd++;
        }
      }
      Even=Arrays.copyOfRange(Even,0,even);
      Odd=Arrays.copyOfRange(Odd,0,odd);
      System.out.println("Even: "+even+" ");
      System.out.println(Arrays.toString(Even));
      System.out.println("Odd:  "+odd+" ");
      System.out.println(Arrays.toString(Odd));
     }
    }   
    

    Note: I have used Arrays.copyOfRange(array,start,end) function to copy a certain part of the array from start to end-1 position.

    Output:

    Array after reverse:

    [13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

    Even: 6

    [2, 4, 6, 8, 10, 12]

    Odd: 7

    [1, 3, 5, 7, 9, 11, 13]

    Hope this helps :)

    --Mathan Madhav