Home > Mobile >  Why is the for loop assigning 0 in the array?
Why is the for loop assigning 0 in the array?

Time:02-22

Need to separate odds and evens from an array and put them into their own array. The for loops keep adding a 0 to the evenA and oddA arrays if the spot is not filled in. Cannot figure this one out.

    //Figure out length of odd and even array

    for(int i = 0; i < size; i  ) {
        if ((arr[i] % 2) == 0) {
            evens  = 1;
        }
        else {
            odds  = 1;
        }
    }

    //Create new arrays for both even and odds

    int[] evenA = new int[evens];
    int[] oddA = new int[odds];
    
    //Add the even numbers and odd numbers to the arrays.

    for(int i = 0; i < size; i  ) {
        if ((arr[i] % 2) == 0) {
            evenA[j] = arr[i];
        }
        else {
                oddA[j] = arr[i];
        }
    }

Example Output:

    Array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}
    
    evenA[] = {0, 2, 0, 4}
    oddA[] = {1, 0, 3, 0, 5}

Then, it stops running. I need to get rid of the zeroes.

CodePudding user response:

It is not putting zeros into the array. The array was filled with zeros and both array indexes are being incremented. So, the values are being put into every other index location. To fix, track the indexes of even and odd arrays separately:

int oddPtr = 0;
int evenPtr = 0;
for(int i = 0; i < size; i  ) {
    if ((arr[i] % 2) == 0) {
        evenA[evenPtr  ] = arr[i];
    }
    else {
        oddA[oddPtr  ] = arr[i];
    }
}

Lastly, the size of both even and odd arrays should be approximately half of the original array.

UPDATE:

I can't believe some of the comments below the OP's question. So I am explaining more details about the zeros in the arrays. The line:

int[] evenA = new int[evens];

creates an array of length evens (lets assume 4), where every element in the array is initialized with the default value of int data type; which is zero. Therefore, evenA looked like:

evenA => {0,0,0,0};

Because the index counter (pointer) was incremented every iteration AND it was shared between the two arrays, the insertion of odd and even values was interlaced with respect to each other:

{0,2,0,4,0}
{1,0,3,0,5}

So, every odd index location contained even numbers and even index locations contained odd numbers as illustrated above. That is why the solution I provided contains two index pointers so that each is used and incremented only when the conditions for determining odd or even is matched.

CodePudding user response:

As you are using the same counter for both arrays your array are getting filled inconsistently. You are using the same j variable for both array assignment. For example, if u have the following array

int[] arr = {1, 2, 3, 4};

And j = 0; intially. Your loop would look something like this:

First iteration:

oddA = {0, 0}; evenA = {0, 0};

j = 0; i = 0;

As the first element in the array is odd: j = 1; oddA = {1, 0}; evenA = {0, 0};

The next iteration:

oddA = {1, 0}; evenA = {0, 0};

j = 1; i = 1;

As the second element in the array is even:

j = 1; oddA = {1, 0};

evenA[j] = 2 => evenA = {0, 2};

Instead you should use two different counters for your evenA and oddA arrays.

int[] arr = {1, 2, 3, 4};
    int evens = 0, odds = 0;
    for (int num : arr) {
        if (num % 2 == 0) evens  ;
        else odds  ;
    }

    int[] evenA = new int[evens];
    int[] oddA = new int[odds];

    // Creating separate array counters
    int evenArrayCounter = 0;
    int oddArrayCounter = 0;

    for (int num : arr) {
        if (num % 2 == 0) evenA[evenArrayCounter  ] = num;
        else oddA[oddArrayCounter  ] = num;
    }
  • Related