Total members 10261 | Gratitudes |It is currently Tue May 22, 2012 5:45 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 2 posts ] 
Author Question
 Question subject: Average of an array. Please help
PostPosted: Sun Mar 27, 2011 8:53 pm 
Offline
Newbie
User avatar

Joined: Sun Mar 27, 2011 8:47 pm
Posts: 1
Has thanked: 1 time
Have thanks: 0 time

In the method below trying to calculate average age in each category('o','u','n'). Ages are stored in a separate class.

Code:
public double averageAge() // calculating average age in each BMI group
    
{   
        
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[0]){
            
result=result+b;
            
numbers[j]++;}
        }
}       for (
int j=0;j<3;j++) 
        
System.out.println("Average age in group "+cat[j]+ " is "+result/this.list.length);
        return 
result;
}
 

Result returns 0.. Any ideas how this can be fixed?


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Average of an array. Please help
PostPosted: Sun Mar 27, 2011 9:07 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time
hey brother ,

in line
Code:

          
if(b==cat[0]){
            
result=result+b;
            
numbers[j]++;}
        }
 


this is wrong !. car is array of char , in condition b is (Integer) , so you are comparing the ASCII code of 'o' and the value of integer b . it seems you don't enter this condition ,
i think you should has three counter. one for each group and also a sum for each group . and add conditions in for loop for handling the three cases .

and after all , you calculate the average = sum/counter.

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )



For this message the author msi_333 has received gratitude : deto4ka
TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 2 posts ] 
Quick reply


  

 Similar topics
 search for string in a cell array
 Shuffle Array
 check array contains a value using javascript
 Array size to zero
 array in oracle database
 How to write C++ code that defines array of size 24 then;
 access element in array C++
 need some help with array comparing program (solved)
 PHP SOAP server returns array using NuSOAP Library
 java Array.! (help needed for brginner)

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: MSNbot Media and 7 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team