'The wrong data is displayed in the output on c

Taking a sequence of (non-empty) integers the program should first request the number of integers to read and dynamically allocate an array large enough to hold the number of values you read. You should then loop in the elements of the array. Then, your program must use malloc() to dynamically allocate two arrays, one to hold all the even numbers in the array you just read, and one to hold the odd numbers in the array. You must allocate just enough space for each array to hold odd and even numbers.

That is my test case

Please enter the number of times you want to enter the temperature:
4
Please enter the numbers:
21
40
31
50
odds are: 21
odds are: 0
evens are: 0
evens are: 40

This is my code:

#include <stdio.h>
#include<stdlib.h> // for malloc

int main(void) {
    int counts = 0; // set a variable named counts recored how many of the times
    printf("Please enter the number of times you want to enter the temperature: \n");
    scanf("%d",&counts);
    int *odd_evens;
    odd_evens = malloc(sizeof(int)*(counts));
    printf("Please enter the numberss: \n");
    for (int i = 0; i < counts; i++) { // use for loop to read temperature
        scanf("%d",&odd_evens[i]); // record the temperature
    }
    int odds_number = 0; // calcuate how many numbers are odds
    int evens_number = 0; // calcuate how many numbers are evens
    for (int i = 0; i < counts; i++) {
        if (odd_evens[i] %2 == 0) {
            odds_number++; // odds add one
        }
        else if (odd_evens[i] %2 != 0) {
            evens_number++; // evens add one
        }
    }
    int *odds;
    odds = malloc(sizeof(int)*(odds_number)); // create dunamic array for odds
    int *evens;
    evens = malloc(sizeof(int)*(evens_number)); // create dunamic array for evens
    for (int j = 0; j < counts; j++) {
        if (odd_evens[j] % 2 == 0) {
            evens[j] = odd_evens[j];
        } 
        else if (odd_evens[j] % 2 != 0) {
            odds[j] = odd_evens[j];   
        }
    }

    for(int m = 0; m < odds_number; m++) {
        printf("odds are: %d\n",odds[m]);
    }
    for (int n = 0; n < odds_number; n++) {
        printf("evens are: %d\n",evens[n]);
    }

    free(odd_evens);
    free(odds);
    free(evens);
    return 0;
}

In my limited coding experience, this usually happens with invalid subscripts, but in my test code, the odd numbers are of length 2 and the even numbers are of length 2. The array range should be correct. Why does this happen?



Solution 1:[1]

One major problem is this loop:

for (int j = 0; j < counts; j++) {
    if (odd_evens[j] % 2 == 0) {
        evens[j] = odd_evens[j];
    } 
    else if (odd_evens[j] % 2 != 0) {
        odds[j] = odd_evens[j];   
    }
}

Here you will go out of bounds of both evens and odds (leading to undefined behavior) since you use the index for odd_evens which most likely will be larger (unless all input is only odd, or only even).

You need to keep separate indexes for evens and odds:

unsigned evens_index = 0;
unsigned odds_index = 0;

for (int j = 0; j < counts; j++) {
    if (odd_evens[j] % 2 == 0) {
        evens[evens_index++] = odd_evens[j];
    } 
    else if (odd_evens[j] % 2 != 0) {
        odds[odds_index++] = odd_evens[j];   
    }
}

Another problem is the printing of the even numbers:

for (int n = 0; n < odds_number; n++) {
    printf("evens are: %d\n",evens[n]);
}

Here you use the odds_number size instead of evens_number.

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 Some programmer dude