hey all,

still very new, i'm trying to create an array and then get an average of the numbers. this is for a class of mine and a friend says i'm close but the book doesnt really give any further help. can you tell me where i'm going wrong?

mucho gracias

``````public class AvgArray
{
// Array
int[] a = {1,2,3,4,5,6};
double[] b = {6.0,4.4,1.9,2.9,3.4,3.5};
double sum;

public static int averagea(int[] array)
{
int sum = 0; //all elements together
for (int i = 0; i < a.length; i++)
{
sum += a[i];
}
return sum / a.length;
}

public static double averageb(double[] array)
{
double sum = 0; //add all of nums
for (double i = 0; i < b.length; i++)
{
sum += b[i];
}
return sum / b.length;
}

/**Main Method*/
public static void main(String args[])
{

}
}``````
3
Contributors
4
Replies
7
Views
10 Years
Discussion Span
Last Post by leroi green

I changed method avaragea to return double if you wish you can correct it back to int

``````public class AvgArray
{
// Array
static int[] a = {1,2,3,4,5,6};
static double[] b = {6.0,4.4,1.9,2.9,3.4,3.5};
static double sum;

public static double averagea(int[] array)
{
int sum = 0; //all elements together
for (int i = 0; i < a.length; i++)
{
sum += a[i];
}
return sum / a.length;
}

public static double averageb(double[] array)
{
double sum = 0; //add all of nums
for (int i = 0; i < b.length; i++)
{
sum += b[i];
}
return sum / b.length;
}

/**Main Method*/
public static void main(String args[])
{
System.out.println("Average of array a = " + averagea(a) );

System.out.println("Average of array b = " + averageb(b) );
}
}``````

Closer, but still off by one. The methods are using a and b directly, instead of the array parameter :P

You probably don't even want to define those array at the class level. They aren't related to the calculations themselves. Just define them in main() and use the methods for the calculations. You only need them at the class level if they are state that the class needs to maintain.

Note I also changed the method names to something that describes what they do a little better. "averagea" isn't very clear about the purpose of the method.

I changed method avaragea to return double if you wish you can correct it back to int

``````public class AvgArray
{
// returns the average of an array as an int
public static int averageInt(int[] array)
{
int sum = 0; //all elements together
for (int i = 0; i < [B]array[/B].length; i++)
{
sum += [B]array[/B][i];
}
return sum / [B]array[/B].length;
}

// returns the average of an array as a double
public static double averageDouble(double[] array)
{
double sum = 0; //add all of nums
for (int i = 0; i < [B]array[/B].length; i++)
{
sum += [B]array[/B][i];
}
return sum / [B]array[/B].length;
}

/**Main Method*/
public static void main(String args[])
{
// Array
int[] a = {1,2,3,4,5,6};
double[] b = {6.0,4.4,1.9,2.9,3.4,3.5};

System.out.println("Average of array a = " + averageInt(a) );

System.out.println("Average of array b = " + averageDouble(b) );
}
}``````

Bleh, just realized I edited the code in peter's post instead of a separate code posting.

``````public class AvgArray
{
// returns the average of an array as an int
public static int averageInt(int[] array)
{
int sum = 0; //all elements together
for (int i = 0; i < [B]array[/B].length; i++)
{
sum += [B]array[/B][i];
}
return sum / [B]array[/B].length;
}

// returns the average of an array as a double
public static double averageDouble(double[] array)
{
double sum = 0; //add all of nums
for (int i = 0; i < [B]array[/B].length; i++)
{
sum += [B]array[/B][i];
}
return sum / [B]array[/B].length;
}

/**Main Method*/
public static void main(String args[])
{
// Array
int[] a = {1,2,3,4,5,6};
double[] b = {6.0,4.4,1.9,2.9,3.4,3.5};

System.out.println("Average of array a = " + averageInt(a) );

System.out.println("Average of array b = " + averageDouble(b) );
}
}``````

mucho thanx everyone. got it...down pat

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.