'print evens number and odds number from an array

Declare an array containing these number and print the evens numbers and odd numbers Now I initialized an array that containing 11 integers. Here is my code

#include <stdio.h>

int main(void) {
int nums[11] = {11,3,9,7,6,10,13,17,2,8,3}; // create an variables that store integers
int evens[11] = {0}; // initialized an array to store even numbers
int odds[11] = {0};  // initialized an array to store even numbers

int length = sizeof(nums) / sizeof(nums[0]); // get the length of nums
int nums_index = 0;
int evens_index = 0;
int odds_index = 0;

for (nums_index; nums_index < length;nums_index++) {
    if (nums[nums_index] % 2 == 0) {
        evens[evens_index] = nums[nums_index];
        evens_index++;
    }
    else if(nums[nums_index] % 2 != 0) {
        odds[odds_index] = nums[nums_index];
        odds_index++;
    }
    printf("%d\n",evens[evens_index]);
    printf("%d\n",odds[odds_index]);
}

return 0;
}

The major question is whether the output has problems when I compile my code. The output is :0 11 0 3 0 9 0 7 6 0 10 0 0 13 0 17 2 0 8 0 0 3 Why it could happened? Thank you all.

c


Solution 1:[1]

You need separate indexing for each array, advancing the index for evens and odds only when nums[i] value is one of the two.

Otherwise you would get sort of a copy of nums with zeroes in place of those numbers of the opposite type (odd/even).

For instance:

int j = 0;
int k = 0;
for (int i = 0; i < length; i++) {
    if (nums[i] % 2 == 0) {
        evens[j] = nums[i];
        j++;
    }
    else if(nums[i] % 2 != 0) {
        odds[k] = nums[i];
        k++;
    }
    printf("%d\n",evens[i]);
    printf("%d\n",odds[i]);
}

This will compose the arrays like:

11 3 9 7 13 17 3 0 0 0 0 --- for odds

6 10 2 8 0 0 0 0 0 0 0 0 --- for evens

The second problem is that you are printing inside the loop, firstly a value from evens and immediately after a value for odds. So if you want to display them nice and separate, you can move both printf outside the first loop, then looping again on each result array for displaying it completely, before proceding to the other.

Solution 2:[2]

#include <stdio.h>

void PrintNumbers(int*, int);

int main(void) {
    int nums[11] = {11,3,9,7,6,10,13,17,2,8,3}; // create an variables that store integers
    int evens[11] = {0}; // initialized an array to store even numbers
    int odds[11] = {0};  // initialized an array to store even numbers
    
    int length = sizeof(nums) / sizeof(nums[0]); // get the length of nums
    int nums_index = 0;
    int evens_index = 0;
    int odds_index = 0;

    for (nums_index; nums_index < length; nums_index++)
    {
        if (nums[nums_index] % 2 == 0)
        {
            evens[evens_index] = nums[nums_index];
            evens_index++;
        }
        else if(nums[nums_index] % 2 != 0)
        {
            odds[odds_index] = nums[nums_index];
            odds_index++;
        }
    }
    
    printf("Original List: ");
    PrintNumbers(nums, length);
    printf("Even numbers: ");
    PrintNumbers(evens, length);
    printf("Odd numbers: ");
    PrintNumbers(odds, length);

    return 0;
}

void PrintNumbers(int* numbers, int n)
{
    for (int i = 0; i < n; i++)
    {
        printf("%d, ", numbers[i]);
    }
    
    printf("\n");
}

Output:

Original List: 11, 3, 9, 7, 6, 10, 13, 17, 2, 8, 3,
Even numbers: 6, 10, 2, 8, 0, 0, 0, 0, 0, 0, 0, 
Odd numbers: 11, 3, 9, 7, 13, 17, 3, 0, 0, 0, 0,

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 leodedro
Solution 2 LuisUrzua