Velocity Reviews > Java > Statistics very basic

# Statistics very basic

Pierre8r
Guest
Posts: n/a

 05-02-2008
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

Christian
Guest
Posts: n/a

 05-02-2008
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[i] 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
Guest
Posts: n/a

 05-02-2008
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[i] 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
>

Arved Sandstrom
Guest
Posts: n/a

 05-02-2008
"Christian" <(E-Mail Removed)> wrote in message
news:481b5fea\$0\$25892\$(E-Mail Removed)...
> 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[i] 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

Arne Vajhøj
Guest
Posts: n/a

 05-02-2008
Pierre8r wrote:
> 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.

http://commons.apache.org/math/api-1...e-summary.html

Arne

Chase Preuninger
Guest
Posts: n/a

 05-02-2008
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++;
}
}

Pierre8r
Guest
Posts: n/a

 05-05-2008
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[i]) {
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));
}
}
}