•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 375,174 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,225 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1668 | Replies: 9
![]() |
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Rep Power: 4
Solved Threads: 0
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];
}
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];
}
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Rep Power: 4
Solved Threads: 4
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:
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.
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.
Last edited by Drowzee : Jul 18th, 2005 at 5:22 pm. Reason: Formatting
•
•
Join Date: Jul 2005
Posts: 47
Reputation:
Rep Power: 4
Solved Threads: 0
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];
}
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];
}
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Rep Power: 4
Solved Threads: 4
...
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.
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.
•
•
Join Date: Jun 2005
Posts: 60
Reputation:
Rep Power: 4
Solved Threads: 5
Well you have newer declared i, before refering to it in
just so you know, you havent declared even or odd as array ether.
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.
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Rep Power: 4
Solved Threads: 4
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.
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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
- Can I ghost a RAID array??? (Windows NT / 2000 / XP / 2003)
- Creating dynamic array structures (C++)
- Array limit (C)
- struct dynamic 2d array alloc (C)
- string to integer array transformation (C)
- Array (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Will Keyboard Hooks solve my problem?
- Next Thread: Need help with integers in c++


Linear Mode