User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Array

  #1  
Jul 18th, 2005
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];
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Array

  #2  
Jul 18th, 2005
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.
Last edited by Drowzee : Jul 18th, 2005 at 5:22 pm. Reason: Formatting
Reply With Quote  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: Array

  #3  
Jul 18th, 2005
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];

}
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Array

  #4  
Jul 18th, 2005
...

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.
Reply With Quote  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: Array

  #5  
Jul 18th, 2005
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.
Reply With Quote  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: Array

  #6  
Jul 18th, 2005
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.
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Array

  #7  
Jul 18th, 2005
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.
Reply With Quote  
Join Date: Jul 2005
Location: Iceland
Posts: 104
Reputation: Alvein is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Alvein's Avatar
Alvein Alvein is offline Offline
Junior Poster

Re: Array

  #8  
Jul 18th, 2005
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".
Reply With Quote  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: Array

  #9  
Jul 18th, 2005
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.
Reply With Quote  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Solution Re: Array

  #10  
Jul 19th, 2005
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:13 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC