# Calculating average of an array

• March 27th, 2011, 07:43 AM
Vika
Calculating average of an array
Have written a program for my uni assignment that stores information about fitness club members(such as name, age weight, BMI index etc.) One of the operations that program must perform is to calculate an average BMI index. The method I have written returns value of 0 and can't figure out where the problem lays.

Code Java:

```public double averageBmi()//to calculate average BMI index { double numbers[]=new double[this.list.length]; double result=0; for(int i=0;i<numbers.length;i++){ double b=this.list[i].Bmiindex; result=result+numbers[i]; }   System.out.println("Average BMI is "+result/numbers.length); return result; }```
Any ideas why?
• March 27th, 2011, 07:57 AM
bojanm
Re: Calculating average of an array
numbers is empty !!! wath is double b ?
• March 27th, 2011, 08:01 AM
Vika
Re: Calculating average of an array
double b is accessing list of all Bmiindexes stored in another class
• March 27th, 2011, 08:27 AM
bojanm
Re: Calculating average of an array
but number[] is still empty 0+0=0
meybe rezult=rezult+b
• March 27th, 2011, 08:30 AM
Vika
Re: Calculating average of an array
It works now! Thanks a lot!
• March 27th, 2011, 08:53 AM
Vika
Re: Calculating average of an array
Facing the same issue with the method calculating average age for each group:(
Code Java:

```public double averageAge() // { double numbers[]=new double[this.list.length]; double result=0; char[] cat={'O','U','N'}; for (int i=0;i<this.list.length;i++){ int b=this.list[i].age; for(int j=0;j<3;j++){//loop runs three times, as there are three categories if(b == cat[j]) result=result+b; } } for (int j=0;j<3;j++) System.out.println("Average age in group "+cat[j]+ " is "+result/numbers.length); return result; }```
• March 28th, 2011, 06:00 AM
JavaPF
Re: Calculating average of an array