Average

i don't know how to

calaculate and returns the average(in floating point) of the elments in an int array...

this is what i have so far:

public class O

{

public O()

{

int a[] = { 7, 8, 9, 9, 8, 7 };

{

System.out.print(average(a));

}

}

/**

* method count that uses for_each loop

*

*/

public int average (int a [])

{

int x = 0;

int total = 0;

for(int i : a ){

x++;

total = total + i;

}

return total / x;

}

}

[579 byte] By [Mikez007a] at [2007-11-26 21:43:50]
# 1
Declare total as a double.
dcmintera at 2007-7-10 3:31:10 > top of Java-index,Java Essentials,New To Java...
# 2

> Declare total as a double.

And change the signature of the average method to return a double, not an int.

public double average (int a [])

{

double total = 0.0;

for(int i : a ){

total += i;

}

return total / a.length;

}

SomeoneElsea at 2007-7-10 3:31:10 > top of Java-index,Java Essentials,New To Java...