Arrays of Odd & Even Numbers - Numbers Getting Mixed Up

Hi, so I am having a bit of a problem, but I don't know if it's my code or my compiler that's the issue. My assignment is to ask the user to input integers, while the program stores the even numbers in one array and the odds in another array. When the user puts in 0, the program is supposed to show them how many evens they put in and how many odds, and then list the values in each array.

So I wrote up the program for it quickly and I thought it would work just fine. No such luck. It always got the number of evens and the number of odds correct, but instead of listing the numbers from the arrays, it usually lists the last x numbers the user input (x being equal to the number of elements in that array). I also occasionally get wacky 5+ digit integers appearing at the ends of the output.

Now, the thing is, my code doesn't look like it has anything wrong with it, so I am thinking it may be my compiler. I'm using Xcode on a Mac OS X 10.7. I am not sure how good this software is, but I've been using it for my class, since I no longer have a Windows readily available. Because of this, I am not sure if my code is bad or if my compiler is the real issue.

After a few reiterations and different approaches, I found my first try had the fewest problems, so that is what I am sticking with in hopes of adjusting it to work.

So here is my code, anyways:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;
int main() {
    int input, remainder, even = 0, odd = 0;
    int evenArray[even];
    int oddArray[odd];
    cout << "This program accepts integers until you enter 0.\nPlease enter a value: ";
    cin >> input;
    while (input != 0) {
        remainder = input % 2;
        if (remainder == 0) {
            evenArray[even] = input;
            even++;
        }
        else {
            oddArray[odd] = input;
            odd++;
        }
        cout << "Enter another integer: ";
        cin >> input;
    }
    cout << "\nThe number of evens is " << even << ".\n";
    cout << "The even values are: ";
    for(int i = 0; i < even; i++) {
        cout << evenArray[i] << " ";
    }
    cout << endl;
    cout << "The number of odds is " << odd << ".\n";
    cout << "The odd values are: ";
    for(int i = 0; i < odd; i++) {
        cout << oddArray[i] << " ";
    }
}


And here's an example of it being run:

This program accepts integers until you enter 0.
Please enter a value: 1
Enter another integer: 2
Enter another integer: 3
Enter another integer: 4
Enter another integer: 5
Enter another integer: 5
Enter another integer: 3
Enter another integer: 2
Enter another integer: 4
Enter another integer: 6
Enter another integer: 4
Enter another integer: 3
Enter another integer: 0

The number of evens is 6.
The even values are: 2 4 2 4 6 3 
The number of odds is 6.
The odd values are: 2 4 2 4 1912356720 32767 


I would very much appreciate it if someone could tell me what the heck is going on.
Last edited on
Arrays don't resize automagically.
look up dynamic arrays.

create an int pointer.

assign it to a new array

then you will have to check when it reaches max size, then reallocate more memory for the larger array and copy data over and then reassign the pointer.

example code:
1
2
3
int *myOddData, *myEvenData;
myOddData = new int [Oddsize];
myEvenData = new int [EvenSize];

do your stuff,

you can treat this exactly like an array once its created.
1
2
myOddData[index];
myEvenData[index];


etc...

hope this helps
Hi. Sorry for taking so long to reply. I think I used the dynamic arrays correctly, but I still seem to get the same results. My current program looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

int main() {
    int input, remainder, even = 1, odd = 1;
    
    int *myOddData, *myEvenData; // declaring pointers
    myOddData = new int [odd]; // declaring array for odds of size "odd"
    myEvenData = new int [even]; // declaring array for evens of size "even"
    
    cout << "This program accepts integers until you enter 0.\nPlease enter a value: ";
    cin >> input;
    while (input != 0) {
        remainder = input % 2;
        if (remainder == 0) {
            myEvenData[even] = input;
            even++;
        }
        else {
            myOddData[odd] = input;
            odd++;
        }
        cout << "Enter another integer: ";
        cin >> input;
    }
    cout << "\nThe number of evens is " << even << ".\n";
    cout << "The even values are: ";
    for(int i = 0; i < even; i++) {
        cout << myEvenData[i] << " ";
    }
    cout << endl;
    cout << "The number of odds is " << odd << ".\n";
    cout << "The odd values are: ";
    for(int i = 0; i < odd; i++) {
        cout << myOddData[i] << " ";
    }
    delete[] myOddData;
    delete[] myEvenData;
}


Example of output:

This program accepts integers until you enter 0.
Please enter a value: 5
Enter another integer: 3
Enter another integer: 6
Enter another integer: 7
Enter another integer: 44
Enter another integer: 23
Enter another integer: 8
Enter another integer: 9
Enter another integer: 12
Enter another integer: 0

The number of evens is 4.
The even values are: 9 44 8 12 
The number of odds is 5.
The odd values are: 5 3 7 23 9 


Did I use the dynamic array wrong? I looked up dynamic arrays and didn't really find anything that could tell me what I am doing is incorrect.
Accessing an array out of bounds is an error.
It will not growth to accommodate the extra elements, you need to handle that logic.

You will be better using std::vector and adding elements with push_back()
I looked at using vector, and it seems like its methods would be perfect for this assignment, but I don't think it complies with the assignment's requirements. The assignment says I have to create an array for odds and an array for evens. Is there no way I can use a dynamic array to the same ends?

Also, I modified the previous program a bit to tell what the last odd and the last even entered were while inside the loop. Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;

int main() {
    int input, remainder, odd = 0, even = 0;
    
    int *myEvenData, *myOddData;
    myEvenData = new int [even];
    myOddData = new int [odd];
    
    cout << "This program accepts integers until you enter 0.\nPlease enter a value: ";
    cin >> input;
    while (input != 0) {
        remainder = input % 2;
        if (remainder == 0) {
            myEvenData[even] = input;
            even++;
        }
        else {
            myOddData[odd] = input;
            odd++;
        }
        cout << "Odd: " << myOddData[odd - 1] << endl;
        cout << "Even: " << myEvenData[even - 1] << endl;
        cout << "Enter another integer: ";
        cin >> input;
    }
    
    cout << "\nThe number of odds is " << odd << ".\n";
    cout << "The odd values are: ";
    for(int i = 0; i < odd; i++) {
        cout << myOddData[i] << " ";
    }
    delete[] myOddData;
    
    cout << "\nThe number of evens is " << even << ".\n";
    cout << "The even values are: ";
    for(int i = 0; i < even; i++) {
        cout << myEvenData[i] << " ";
    }
    delete[] myEvenData;

    
    return 0;
}

And a sample run:

This program accepts integers until you enter 0.
Please enter a value: 100
Odd: 0
Even: 100
Enter another integer: 90
Odd: 0
Even: 90
Enter another integer: 80
Odd: 0
Even: 80
Enter another integer: 70
Odd: 70
Even: 70
Enter another integer: 60
Odd: 70
Even: 60
Enter another integer: 50
Odd: 70
Even: 50
Enter another integer: 40
Odd: 70
Even: 40
Enter another integer: 30
Odd: 70
Even: 30
Enter another integer: 20
Odd: 70
Even: 20
Enter another integer: 10
Odd: 70
Even: 10
Enter another integer: 1
Odd: 1
Even: 10
Enter another integer: 0

The number of odds is 1.
The odd values are: 1 
The number of evens is 10.
The even values are: 100 90 80 70 1 50 40 30 20 10 

Notice how the 70 gets assigned to both the odd and even in the loop. This sort of thing happens fairly often when I run it and input random numbers. Something worth noting is that it always lists the odd numbers correctly at the end, but the list of evens often has odds mixed into it. I am not getting any sort of errors, so I can't quite figure out what is going on here. Can anybody explain why the issue is so specific? Like, if this is happening at all, shouldn't it happen to both the evens and odds?
Last edited on
> I am not sure if my code is bad or if my compiler is the real issue.

When your program does not work as expected, start with the axiom: my code is broken.

You are not re-sizing the arrays to accommodate more numbers as they are added.

This example uses one resizeable array to hold all the numbers entered by the user:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

int main()
{
    int size = 0 ; // count of numbers entered by the user
    int available = 16 ; // the amount of space available in the array
    int* array = new int[ available ] ; // the initial array

    int number ;
    // for each number entered by the user till a zero is enterede
    while( std::cout << "? " && std::cin >> number && number != 0 )
    {
        if( size == (available-1) ) // there is no space available in the array
        {
            // create a larger array (twice as big as the current one)
            available *= 2 ;
            int* temp = new int[ available ] ;

            // copy the contents of the current array into the larger one
            for( int i=0 ; i<size ; ++i ) temp[i] = array[i] ;

            // destroy the old array, we do not need it any longer
            delete[] array ;
            // and make the new array the array that we work with
            array = temp ;
        }

        array[size] = number ; // add the number to end of array
        ++size ; // and increment the count of numbers
    }
} 


In your program, you need two arrays instead of one; one for even numbers and another for odd numbers.

Beginners, just starting to learn C++, shouldn't be writing this kind of code at all; they should be using the standard library.

See how much easier it is when low-level details (in this case management of dynamically allocated memory) are abstracted away:
1
2
3
4
5
6
7
8
9
10
11
12
#include <vector>
#include <iostream>

int main()
{
    std::vector<int> vec ; // create an empty vector
    int number ;

    // for each number entered by the user till a zero is enterede
    while( std::cout << "? " && std::cin >> number && number != 0 )
          vec.push_back(number) ; // add the number to the end of the vector
}
When your program does not work as expected, start with the axiom: my code is broken.

Heh. I suppose that would be best. :P

Anyways, I integrated your example of a resizable array into my program, replacing array with oddArray or evenArray, number with input, size with odd or even, and available with oddAvailable or evenAvailable. I still get the problem of numbers crossing over between odds and evens (also worth noting: this time the list of odds contains even numbers; maybe this is just my first time noticing though).

Beginners, just starting to learn C++, shouldn't be writing this kind of code at all; they should be using the standard library.

I am sure you are right, but this is the last assignment of my introductory C++ course. So perhaps the professor wished my class to think outside the box. Only problem is, after my professor looked at my original code, he couldn't find anything wrong with it, which is odd because now that I look at it after discussing it, it is pretty clear what I was doing wasn't going to work.

I think I will just use vectors for the assignment as you and ne555 suggested. I just ran it as such and it works beautifully.

Although, if anyone can figure out why the numbers keep getting switched between the odd and even arrays, even after using JLBorges' method of resizing, I would very much like to hear it.
Last edited on
> Although, if anyone can figure out why the numbers keep getting switched between the odd and even arrays

Can only make a wild guess without seeing the code.

Did you initialize oddAvailable and evenAvailable with some non-zero positive value?
Oh, apologies. Yes, I did. Here is that version of my code as it is now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
int main()
{
    int input, remainder, odd = 0, oddAvailable = 16, even = 0, evenAvailable = 16;
    int* evenArray = new int [even];
    int* oddArray = new int [odd];
    cout << "This program accepts integers until you enter 0.\nPlease enter a value: ";
    cin >> input;
    while (input != 0)
    {
        remainder = input % 2;
        if (remainder == 0)
        {
            if (even == evenAvailable - 1)
            {
                evenAvailable *= 2;
                int* temp = new int[evenAvailable];
                for( int i=0 ; i < even ; ++i )
                    temp[i] = evenArray[i];
                delete[] evenArray;
                evenArray = temp;
            }
            evenArray[even] = input;
            ++even;
        }
        else
        {
            if (odd == oddAvailable - 1)
            {
                oddAvailable *= 2;
                int* temp = new int[oddAvailable];
                for( int i=0 ; i < odd ; ++i )
                    temp[i] = oddArray[i];
                delete[] oddArray;
                oddArray = temp;
            }
            oddArray[odd] = input;
            ++odd;
        }
        cout << "Odd: " << oddArray[odd - 1] << endl;
        cout << "Even: " << evenArray[even - 1] << endl;
        cout << "Enter another integer: ";
        cin >> input;
    }
    cout << "\nThe number of odds is " << odd << ".\n";
    cout << "The odd values are: ";
    for(int i = 0; i < odd; i++)
        cout << oddArray[i] << " ";
    delete[] oddArray;
    
    cout << "\nThe number of evens is " << even << ".\n";
    cout << "The even values are: ";
    for(int i = 0; i < even; i++)
        cout << evenArray[i] << " ";
    delete[] evenArray;
    return 0;
}

And output:

This program accepts integers until you enter 0.
Please enter a value: 34
Odd: 0
Even: 34
Enter another integer: 765
Odd: 765
Even: 34
Enter another integer: 342
Odd: 765
Even: 342
Enter another integer: 43
Odd: 43
Even: 342
Enter another integer: 2
Odd: 43
Even: 2
Enter another integer: 90
Odd: 43
Even: 90
Enter another integer: 85
Odd: 85
Even: 90
Enter another integer: 77
Odd: 77
Even: 90
Enter another integer: 100
Odd: 77
Even: 100
Enter another integer: 99
Odd: 99
Even: 100
Enter another integer: 66
Odd: 99
Even: 66
Enter another integer: 41
Odd: 41
Even: 66
Enter another integer: 12
Odd: 41
Even: 12
Enter another integer: 0

The number of odds is 6.
The odd values are: 100 66 12 77 99 41 
The number of evens is 7.
The even values are: 34 342 2 90 100 66 12 
You start with zero-size arrays.
It should be int* evenArray = new int [evenAvailable];
Oh wow. I feel dumb. >_> It works perfectly now. Thank you very much. Problem solved. :D

New output:

This program accepts integers until you enter 0.
Please enter a value: 45
Odd: 45
Even: 0
Enter another integer: 435
Odd: 435
Even: 0
Enter another integer: 6543
Odd: 6543
Even: 0
Enter another integer: 7
Odd: 7
Even: 0
Enter another integer: 5
Odd: 5
Even: 0
Enter another integer: 6
Odd: 5
Even: 6
Enter another integer: 3245
Odd: 3245
Even: 6
Enter another integer: 654
Odd: 3245
Even: 654
Enter another integer: 4
Odd: 3245
Even: 4
Enter another integer: 90
Odd: 3245
Even: 90
Enter another integer: 100
Odd: 3245
Even: 100
Enter another integer: 21
Odd: 21
Even: 100
Enter another integer: 11
Odd: 11
Even: 100
Enter another integer: 0

The number of odds is 8.
The odd values are: 45 435 6543 7 5 3245 21 11 
The number of evens is 5.
The even values are: 6 654 4 90 100 
Topic archived. No new replies allowed.