# Statistics very basic

Discussion in 'Java' started by Pierre8r, May 2, 2008.

1. ### Pierre8rGuest

Hello,

I have a array like this :

double[] array = { 10.1, 34.0, 15.0, 22.5, 24.2, 31.0, 32.0, 37.0 };

I search to know
how many values there are :
<10
how many values there are between :
>= 10 and < 20

how many values there are between :
>= 20 and < 30

how many values there are :
>= 30

Regards,

Pierre8r

Pierre8r, May 2, 2008

2. ### ChristianGuest

Pierre8r schrieb:
> Hello,
>
> I have a array like this :
>
> double[] array = { 10.1, 34.0, 15.0, 22.5, 24.2, 31.0, 32.0, 37.0 };
>
> I search to know
> how many values there are :
> <10
> how many values there are between :
> >= 10 and < 20

> how many values there are between :
> >= 20 and < 30

> how many values there are :
> >= 30

>
> Regards,
>
> Pierre8r

Homework is meant to be done by you. It should help you to learn the
basics of java.

2 tips:
1. array you can access the i-th index of the array..
2. You will want to use a for-loop to get all positions in the array

Christian, May 2, 2008

3. ### Pierre8rGuest

Christian a écrit :
>> Pierre8r

> Homework is meant to be done by you. It should help you to learn the
> basics of java.
>

The true question is :
How to use :
package org.apache.commons.math.stat; to do the job ?

Cant org.apache.commons.math.stat do this kind of stuff ?
It's more about how to use org.apache.commons.math.stat, because I will
need other functions from this API.
This question is just a start.

I have already try to use org.apache.commons.math.stat, but without success.

I have try Frequency but without success.

> 2 tips:
> 1. array you can access the i-th index of the array..
> 2. You will want to use a for-loop to get all positions in the array
>

Pierre8r, May 2, 2008
4. ### Arved SandstromGuest

"Christian" <> wrote in message
news:481b5fea\$0\$25892\$...
> Pierre8r schrieb:
>> Hello,
>>
>> I have a array like this :
>>
>> double[] array = { 10.1, 34.0, 15.0, 22.5, 24.2, 31.0, 32.0, 37.0 };
>>
>> I search to know
>> how many values there are :
>> <10
>> how many values there are between :
>> >= 10 and < 20

>> how many values there are between :
>> >= 20 and < 30

>> how many values there are :
>> >= 30

>>
>> Regards,
>>
>> Pierre8r

> Homework is meant to be done by you. It should help you to learn the
> basics of java.
>
> 2 tips:
> 1. array you can access the i-th index of the array..
> 2. You will want to use a for-loop to get all positions in the array

I'll just add to that, you only need to look at each value once. Also, for
this particular case, use of some functions in java.lang.Math will avoid the
use of conditionals.

AHS

Arved Sandstrom, May 2, 2008
5. ### Arne VajhøjGuest

Arne Vajhøj, May 2, 2008
6. ### Chase PreuningerGuest

Re: Statistics very basic

for each one create a counter and loop through the array then if the
number meats the required pram increment the counter

//<10
double[] array = { 10.1, 34.0, 15.0, 22.5, 24.2, 31.0, 32.0, 37.0 };
int count = 0;
for(double d : array)
{
if(d > 10)
{
count++;
}
}

Chase Preuninger, May 2, 2008
7. ### Pierre8rGuest

Thaks to Chase, for the loop tip.

I have find something it seen work.
If you have something better, please post.

Pierre8r

import org.apache.commons.math.stat.Frequency;

public class TestStats001 {

/**
* @param args
*/

public static void main(String[] args) {
Frequency frequency = new Frequency();
double[] population = { 10.1, 34.0, 10.0, 22.5, 24.2, 1.0, 32.0, 37.0 };
double[] caracteres = { 10.0, 20.0, 30.0 };
boolean find;

for (double p : population) {
int i;
find = false;
for (i = 0; i < caracteres.length; i++) {
if (p < caracteres) {
find = true;
break;
}
}
if (find == false) {
}
}

for (int i = 0; i < caracteres.length + 1; i++) {
System.out
.println("Caractère " + i + " : " + frequency.getCount(i));
}
for (int i = 0; i < caracteres.length + 1; i++) {
System.out
.println("Pourcentage " + i + " : " + frequency.getPct(i));
}
}
}

Pierre8r, May 5, 2008