# Count the number of even and odd Values in a given array

Page 1 of 1

## 10 Replies - 54791 Views - Last Post: 05 December 2011 - 06:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=258502&amp;s=21c784aa2c6eed4016e24f2a220f1931&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 swim_5318

Reputation: 1
• Posts: 84
• Joined: 05-December 11

# Count the number of even and odd Values in a given array

Posted 05 December 2011 - 02:37 AM

Count the number of even and odd elements in an array of integers. Since you must return two counts, place them into an array of length 2. For example, if you are given the array
1, 4, 9, 16, 25
you return the array with elements
2, 3
since there were two even and three odd elements.

Here is what I have gotten. It compiles, but doesn't count the even or the odds. It just gives me zeros....
```public class Numbers
{
/**
Computes the number of even and odd values in a given array
@param values an array of integer values
@return an array of length 2 whose 0 entry contains the count
of even elements and whose 1 entry contains the count of odd
values
*/
public int[] evenOdds(int[] values)
{
int[] evenOdds= new int[2];
int oddCount=0;
int evenCount=0;
for(int i=0; i<values.length; i++)
{
if(values[i]%2==1);
oddCount++;
if(values[i]%2==0);
evenCount++;
}

values[0]=evenCount;
values[1]=oddCount;
return evenOdds;
}
}

```

This post has been edited by macosxnerd101: 05 December 2011 - 08:19 AM
Reason for edit:: Please use code tags

Is This A Good Question/Topic? 0

## Replies To: Count the number of even and odd Values in a given array

### #2 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

## Re: Count the number of even and odd Values in a given array

Posted 05 December 2011 - 02:43 AM

You never assigned evenOdds with oddCount or evenCount

This post has been edited by Mylo: 05 December 2011 - 02:44 AM

### #3 swim_5318

Reputation: 1
• Posts: 84
• Joined: 05-December 11

## Re: Count the number of even and odd Values in a given array

Posted 05 December 2011 - 02:48 AM

Isn't the values[0]=evenCount; values[1]=oddCount; assigning it to the evenOdd array? If not, how would I assign them to the evenOdd?

### #4 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

## Re: Count the number of even and odd Values in a given array

Posted 05 December 2011 - 02:51 AM

No, values[] and oddEven[] are not the same variables. Values[] is what the user entered. You were just overwriting the array that was passed in.

oddEven[0]=evenCount;
oddEven[1]=oddCount;

### #5 swim_5318

Reputation: 1
• Posts: 84
• Joined: 05-December 11

## Re: Count the number of even and odd Values in a given array

Posted 05 December 2011 - 02:58 AM

Thank you! That worked, but now it is counting all of the numbers...

### #6 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

## Re: Count the number of even and odd Values in a given array

Posted 05 December 2011 - 03:00 AM

What do you mean, isn't the point to count all the numbers?

### #7 swim_5318

Reputation: 1
• Posts: 84
• Joined: 05-December 11

## Re: Count the number of even and odd Values in a given array

Posted 05 December 2011 - 03:06 AM

Yes but it is suppose to give the number of even numbers and then the number of odd numbers. It is giving the total numbers in the array for both. So for example the parameters were [1,2,3] and the return was [3,3] when it should have been [1,2]. 1 for the number of even in the parameters and 2 for the number of odds in the parameters.

### #8 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

## Re: Count the number of even and odd Values in a given array

Posted 05 December 2011 - 03:14 AM

if(values[i]%2==1); // does nothing
oddCount++;
if(values[i]%2==0); // does nothing
evenCount++;
// so it's doing oddCount++ and evenCount++ every loop.

You should use the {} keys for your ifs, even for one line. However, you have ended the if statement with a ;

### #9 swim_5318

Reputation: 1
• Posts: 84
• Joined: 05-December 11

## Re: Count the number of even and odd Values in a given array

Posted 05 December 2011 - 03:16 AM

ahh! I knew that, thank you so much!! It worked perfect.

### #10 stackoverflow

Reputation: 166
• Posts: 545
• Joined: 06-July 11

## Re: Count the number of even and odd Values in a given array

Posted 05 December 2011 - 04:24 AM

Why not a for-each loop? Simpler to read and you don't need two if's if you're only dealing with even and odds.

```for(int n : numArray){
if (n % 2 == 0)
evenCount++;
else
oddCount++;
}
```

### #11 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Count the number of even and odd Values in a given array

Posted 05 December 2011 - 06:42 PM

Actually you don' even need a if
```int[] oddEvenCount(int[] array) {
int[] result = new int[2];
for(int i = 0; i < array.length; ++i)
++result[array[i] % 2];
return result;
}

```