10 Replies - 6960 Views - Last Post: 05 December 2011 - 06:42 PM Rate Topic: -----

#1 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • 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;
Was This Post Helpful? 1
  • +
  • -

#5 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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...
Was This Post Helpful? 0
  • +
  • -

#6 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • 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?
Was This Post Helpful? 1
  • +
  • -

#7 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • 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 ;
Was This Post Helpful? 2
  • +
  • -

#9 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 stackoverflow  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 164
  • View blog
  • 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++;
}

Was This Post Helpful? 1
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • 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;
}


:^:
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1