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.

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, count, largest, smallest;
cout <<  "enter 10 values:\n";
for (count = 0; count < 10; count++)
{
cout << "Enter an integer value:";
cin >> array[count];
}
``````

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] = …``````

...

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 …

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.

## 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, 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, odd;

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 has legitimate locations in even to even, but trying to access even 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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.