hi, I'm trying to write a program that separate an array into 2 arrays, odd and even numbers. but I don't know how.
can you please help? I'm stuck and don't know how to continue.

This is what i have so far

//this program accepts 10 integers, stores in an array.
//the array will then be separated into 2 arrays of odd and even data values.  

#include <iostream.h>

//prototype
void EvenNumber(int);
void OddNumber(int);

void main(void)
{
    int array[10], count, largest, smallest;
    cout <<  "enter 10 values:\n";
    for (count = 0; count < 10; count++)
    {
        cout << "Enter an integer value:";
        cin >> array[count];
    }

Dani AI

Generated

A few concrete corrections and a tiny, safe example to make the program work reliably.

Common pitfalls visible in the thread: the loop index was missing or started at 1 instead of 0, the even/odd storage arrays were not declared with size, the same array index was reused for both output arrays, and the parity test can be written incorrectly because of operator precedence. was right to suggest the modulus idea; is right to prefer modern headers and int main; is right to warn about separate write indices.

Key rules to follow

  • Use zero-based indexing: loop from 0 to 9 for ten elements.
  • Declare distinct arrays (or vectors) with a known capacity.
  • Keep two write counters (one for evens, one for odds) so you don’t leave holes or overwrite.
  • Test parity as value % 2 == 0 (do not negate the operand before taking % — that changes the meaning).

A simple, correct array-based example:

#include <iostream>

int main() {
    int inputs[10];
    int evenVals[10] = {0}, oddVals[10] = {0};
    int eCount = 0, oCount = 0;

    for (int i = 0; i < 10; ++i) {
        std::cout << "Enter integer: ";
        std::cin >> inputs[i];
        if (inputs[i] % 2 == 0) {
            if (eCount < 10) evenVals[eCount++] = inputs[i];
        } else {
            if (oCount < 10) oddVals[oCount++] = inputs[i];
        }
    }

    std::cout << "Evens:";
    for (int i = 0; i < eCount; ++i) std::cout << ' ' << evenVals[i];
    std::cout << '\n';

    std::cout << "Odds:";
    for (int i = 0; i < oCount; ++i) std::cout << ' ' << oddVals[i];
    std::cout << '\n';

    return 0;
}

Troubleshooting tips: initialize your counters to zero, never write past the array size (check counts), and prefer std::vector<int> once you learn it — it removes manual bounds and simplifies pushes.

Recommended Answers

All 9 Replies

Once you've got the info in count, you can use the % operator, called 'modulus',
to divide the numbers up.

All you need to do is a simple if statement like this:

if(!count[i]%2) // if divisible by 2, then returns zero, negates to 1, enters if 
  even[i] = count[i];
else
 odd[i] = count[i];

Modulus is a very handy operator. What it does is give the remainder of integer division. So, for modulus of 3, (%3), the following translation occurs:
0%3== 0
1%3== 1
2%3== 2
3%3== 0
4%3== 1

And so on.

hi, thanks so much for the reply
however, when i try to run the program, it said "i is an undeclared indentifier."
what do I do?

#include <iostream.h>

void main(void)
{
    int array[10], count, even, odd;
    cout <<  "enter 10 values:\n";
    for (count = 1; count < 10; count++)
    {
        cout << "Enter an integer value:";
        cin >> array[count];
    }

    if(!count[i] % 2) // if divisible by 2, then returns zero, negates to 1, enters if 
     even[i] = count[i];
    else
    odd[i] = count[i];

}

...

This is homework, I take it? 'i' is the generally-used iterative counter variable. It's an integer you've got to declare, then loop the code I gave you, having 'i' increment to advance through the loop.
It's the same sort of thing as count.

I was just making an example case on how to do the seperation once. You've got to figure out how to code it to be useful...

One more edit:
Looking back at the start, you don't have your arrays declared properly, either.

I'd suggest you first go to your books or google 'C++ tutorial array', because that'll be of more help than I will be.

Well you have newer declared i, before refering to it in

if(!count[i] % 2) // if divisible by 2, then returns zero, negates to 1, enters if 
even[i] = count[i];
else
odd[i] = count[i];

just so you know, you havent declared even or odd as array ether.

yeah, it (and along with 3 more programs) gives me a headache all morning.

can you help please? I can't take much more programming.

zyrus is right. You can't declare your arrays like that. You need to have the array size after each one.

int even[10], odd[10];

Otherwise, your program only allocates memory for one integer, not 10.

Oh: just for an example to head off what I can see coming: array indexing (where you are in the array) goes from 0 to the size of the array -1.

So, even[10] has legitimate locations in even[0] to even[9], but trying to access even[10] will give you an error.

Just another quick detail. Don't use the same index (i) for both even[] and odd[] arrays. That will left some positions filled with "garbage".

small tips:#include <iostream.h> is old style, use <iostream>, and main shuld return int (int main).

Ok, I dont know if you have loearnt about vectors, but if so, they wold be a better choise for this programe than array.

thanks guys,
you guys have been a lot of help.
and no I haven't learnt about vectors yet.
I'm a level-1 C++ learner, I have to write everytime out the long way, since I don't know much what C++ could do. so even the simplest program give me a header.

Thanks again,
Karen

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.