how to count numbers under 5.5

• 05-10-2011, 03:57 AM
pinotje
how to count numbers under 5.5
Hi all,

Code:

`double numbers[] = {9.8, 5.3, 6.2, 5.5, 5.4, 7.7, 8.1, 3.7, 1.8, 4.6};`
how can I count numbers thats 5.5 or lower?
in the example above it must be 5 numbers under 5.5

Thanks
• 05-10-2011, 04:00 AM
Fubarable
Quote:

Originally Posted by pinotje
Hi all,

Code:

`double numbers[] = {9.8, 5.3, 6.2, 5.5, 5.4, 7.7, 8.1, 3.7, 1.8, 4.6};`
how can I count numbers thats 5.5 or lower?
in the example above it must be 5 numbers under 5.5

Thanks

What do you think the steps should be? Can we see your attempt?
• 05-10-2011, 04:06 AM
pinotje
Quote:

Originally Posted by Fubarable
What do you think the steps should be? Can we see your attempt?

I really don't know...

I have tried this:
Code:

```if(numbers[i] < 5.5) { count++ } System.out.println ("counted: " +count);```
Which is certainly wrong...
• 05-10-2011, 04:10 AM
Fubarable
You need two constructs for this to work. First you have to go through all of the numbers in the array -- what code structure will allow you to loop through each item in the array?
• 05-10-2011, 04:12 AM
pinotje
Quote:

Originally Posted by Fubarable
You need two constructs for this to work. First you have to go through all of the numbers in the array -- what code structure will allow you to loop through each item in the array?

Do you mean this?:
Code:

`for(int i=0; i<numbers.length; i++)`
• 05-10-2011, 04:15 AM
Fubarable
Quote:

Originally Posted by pinotje
Do you mean this?:
Code:

`for(int i=0; i<numbers.length; i++)`

Yes, exactly, a for loop. You've posted the "control" portion of the loop, and the loop will also have a body that will be between curly braces, {}. In the loop body you'll use the if block, and then after the loop you'll print out the results.

Give it a try! :)
• 05-10-2011, 05:36 AM
Junky
Beware when using floating point numbers in equality statements.
Code:

```class DoubleTrouble {     public static void main(String[] args) {         int count = 0;         double[] values = {1, 7.0, 5.4, 5.5, 5.6, 5.499999999999999999999999999999};         for(double d : values) {             if(d < 5.5) {                 count++;             }         }         System.out.println(count);     } }```
You would expect the output of the above code to be 3 but is in fact only 2.
• 05-10-2011, 05:26 PM
pinotje
Quote:

Originally Posted by Fubarable
Yes, exactly, a for loop. You've posted the "control" portion of the loop, and the loop will also have a body that will be between curly braces, {}. In the loop body you'll use the if block, and then after the loop you'll print out the results.

Give it a try! :)

• 05-10-2011, 05:28 PM
pinotje
Quote:

Originally Posted by Junky
Beware when using floating point numbers in equality statements.
Code:

```class DoubleTrouble {     public static void main(String[] args) {         int count = 0;         double[] values = {1, 7.0, 5.4, 5.5, 5.6, 5.499999999999999999999999999999};         for(double d : values) {             if(d < 5.5) {                 count++;             }         }         System.out.println(count);     } }```
You would expect the output of the above code to be 3 but is in fact only 2.

Wow Thanks!

This is exactly what I'm looking for!
• 05-10-2011, 05:31 PM
pinotje
Can anyone tell me the difference between:

Code:

`double[] numbers = new double[]{1.0, 1.5, 2.0, 2.5, 3.0, 3.5};`
,

Code:

`double[] numbers = {1.0, 1.5, 2.0, 2.5, 3.0, 3.5};`
and

Code:

`double numbers[] = {1.0, 1.5, 2.0, 2.5, 3.0, 3.5};`
I've tried all those 3 codes, and they work the same for me...
Are these codes just the same?
• 05-10-2011, 05:53 PM
dpspine420
yes all 3 are ways to do the same thing, although the first one is redundant. I personally prefer
Quote:

double[] numbers = {values};