# Thread: average of an array

1. Member
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

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 = ".");
}
}```

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?

3. Senior 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

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.

4. Member
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.

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];```
This will assign the value 12 to the variable x as the value 12 is stored in the array with an index of 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?

6. ## Re: average of an array

Java Code:
`Mean m = new Mean( );`
Looks like you are just guessing now. You need to clear your mind and think clearly not take wild stabs at it.