OK. I need help with my homework. I a few problems to complete I was able to get pass the first few but I'm stuck. They are progresive problems So If I can get this one going I think I will be OK with the rest.


This is what I have to do.

Create a method called countOdds which takes an integer array as a parameter, and returns the number of odd entries in the array.

This the code we where given and an example of it.

Examples:
theArray:{10, 3, 6, 3, 8, 10} ==> 2
theArray:{11, 3, 6, 7, 9, 60} ==> 4

public int countOdds(int[] data){}

THis is what I have so Far.

int i;
countOdds[] odds = new countOdds[5];
for(i = 0; i < odds.length - 1; i++)
  if countOdds[]%2 = 0;

Im so lost right now. I know I need to use Mod to determine if it is Odd or notbut I not sure how to put this in my code. Any help would be greatful or a guide in the right direction. Thanks

You're definitely on the right track, but a little off. First, the array is given to you as the parameter, so you don't need to make another one. And your logic is slightly off in the if statement. Try something like this:

public int countOdds(int[] data)
{
  int oddCount = 0;
  for(int i = 0; i < data.length; i++)
  {
    if(data[i] % 2 == 1) // if it's odd
      oddCount++;        // increment the counter
  }
  return oddCount;
}

Remember that if statements have the condition in parentheses, and that testing for equality is '==' rather than '='. Also, when you go through an array, you want to index 0 to theArray.size-1; the -1 is done implicitly by using <, but if you use <= you need to make the adjustment.

Comments
Nicely explained...
package mp5;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class Array3 {
    public static void main ( String argv[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter 12 Number : ");
        int num = sc.nextInt(); 
        List<Integer> value = new ArrayList<Integer>(num);
        for (int i = 0; i < num; i++) {
        System.out.print("INPUT " + i + " : ");
        value.add(sc.nextInt());
        }
        int numEvens = 0;
        int numOdds = 0;
        for (int eachValue : value) {
        if (eachValue % 2 == 0 )
        numEvens++;
        else
        numOdds++;
        }
        System.out.println(" ");
        System.out.println("There are " + numOdds + " Odd Numbers");
        System.out.print(" ");
        for (int eachValue : value) {
        if (eachValue % 2 != 0 ) 
            System.out.print(eachValue + " , ");
        }
    }
}

Edited 3 Years Ago by Dani: Formatting fixed

can you write an array that goes from 0 - 99 in all odd numbers? I am having problems and can get the array to print from 0- 99 in multiples of three, but not in all odd numbers. could you give me a hint or help me?

Brian - I'm going to jump in before someone gets cranky at you, and suggest that if you start a new topic, you'll get a lot better response. It's considered bad form to revive old threads to ask new questions.

I'll give you some suggestions in the new topic...

This article has been dead for over six months. Start a new discussion instead.