Results 1 to 6 of 6
Thread: average of an array
 04052013, 02:10 AM #1Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
average of an array
Hi. I'm having trouble finding the average of an array. I was wondering if you could help me.
Error:
Average.java:7: error: '.class' expected
a += A[ ] / A.length;
^( under / )
1 error
What I'm asked to do:
Write a function called Average( ) that takes as a parameter a one dimensional array of doubles called A.
Double Average( double A[ ] )
The function computes and returns the average value of the elements in the array (the sum of the elements in the array divided by the number of elements).
Average:
Java Code:public class Average { public Average() {} double Average( double A[ ] ) { double a = 0; a += A[ ] / A.length; return a; } }
AverageTest:
Java Code:public class AverageTest { public static void main( String [] args ) { Average a = new Average( ); double total; double [] A = { 87.5, 68.1, 94.6, 100.7, 83.9, 78.3 }; for ( int counter = 0; counter < A.length; counter++ ) double total = a.Average(a); System.out.println("Average of the array is " + total = "."); } }
 04052013, 02:15 AM #2
Re: average of an array
You seem to be misunderstanding how an average is calculated. First you add all values up and then divide once at the end. Also what is the empty brackets for?
 04052013, 02:20 AM #3Senior Member
 Join Date
 Feb 2010
 Posts
 128
 Rep Power
 0
Re: average of an array
Is there a particular reason why you declaring an object called Average? Move the Average function out of the Average class and past it under the main function. You want to have the for loop inside the average function.
For each value in the array
update total
end for
return total
Also, your functions are meant to start with a lower case letter and classes with Upper case. As you can see when I say Average, you may not realise what I mean  the function or the class, so follow Java standard.Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
 04052013, 06:13 AM #4Member
 Join Date
 Feb 2013
 Posts
 33
 Rep Power
 0
Re: average of an array
I don't know if I'm suppose to use " Double Average( double A[ ] ) " as it is in the problem.
And I don't know what the empty brackets for.
This is my first time in a java class and first time working with arrays.
I know its all wrong but I'm trying to learn since I majoring in computer science and engineering.
Also do you know where is a really good website that I can use as practice and learn more. Something that is easy to understand.
And I do know what average is... add up all the numbers and divide it by the total of numbers.
I changed my code but its still wrong
Java Code:public class Average { public Average() {} double Mean( double A[ ] ) { double a = 0; double m = 0; a += A[ ]; m = a / A.lenght; return m; } }
Java Code:public class AverageTest { public static void main( String [] args ) { Mean m = new Mean( ); double total; double [] A = { 87.5, 68.1, 94.6, 100.7, 83.9, 78.3 }; for ( int counter = 0; counter < A.length; counter++ ) double total = m.Mean(a); System.out.println("Average of the array is " + total = "."); } }
Thank you for you're help.
 04052013, 06:38 AM #5
Re: average of an array
The reason I asked about the empty brackets was in the hope that you would think about what the correct syntax is. You need to place a value inside the brackets to indicate what index of the array you are trying to access.
Java Code:int[] arr = {12,5,23}; int x = arr[0];
OK so you know how to calculate the average. Writing it in code is not that different. So doing it in your head you would add the first number to a total, then the second, then the third the the fourth etc. Do you see a pattern? What type of statement do you think you need to achieve adding numbers over and over again?
 04052013, 06:40 AM #6
Similar Threads

more accurate average of array data
By jwl in forum New To JavaReplies: 2Last Post: 10282012, 11:14 PM 
Random Array with average
By keki in forum New To JavaReplies: 7Last Post: 04242012, 05:14 PM 
Average of an array. Please help
By vika in forum New To JavaReplies: 1Last Post: 03282011, 09:09 PM 
2 dimensional array, average student
By aborgeld in forum Advanced JavaReplies: 5Last Post: 03052011, 04:12 PM 
Can't get correct average using array
By nevets93 in forum New To JavaReplies: 2Last Post: 02112011, 11:33 AM
Bookmarks