-2

I want to loop through my array list roster and for each Student item I want to return the average of an array of grades. I know there are 3 grades so this code works, but how can I implement this if I don't know how many grades are in the Student's grades array before hand?

    public static void printAverageGrades(){
    System.out.println("Print Average Grades");
    for(Student item : roster)
    {
        double div = roster.size();
        **double average = (item.getGrades()[0] + item.getGrades()[1] + item.getGrades()[2]) / div;**
        System.out.printf("%.2f \n", average);
    }
}
  • So you're saying that `getGrades` returns an `int[]`? – Makoto Apr 23 '17 at 23:41
  • The answer will involve a `for` loop, and possibly `.length` which gives you the length of an array. I'd suggest you find an online tutorial and learn about those. – ajb Apr 23 '17 at 23:43
  • It returns the double properly, but I have to manually enter each position e.g. item.getGrades()[0] + item.getGrades()[1]...I want toloop through the grades array based on the length of the array to return each individual double so that I can average them? – dan_jen1 Apr 23 '17 at 23:44
  • `for (int grade : item.getGrades() {...}` would allow you to sum the grades without knowing the number of grades – MadProgrammer Apr 23 '17 at 23:45
  • Possible duplicate of [How to manipulate arrays. Find the average. Beginner Java](http://stackoverflow.com/questions/12002332/how-to-manipulate-arrays-find-the-average-beginner-java) – Tom Apr 24 '17 at 00:13

2 Answers2

0

You can do this two ways. The more preferable Java 8 way is listed first.

  • Use Arrays.stream to encapsulate the array as a stream, then getting the average is just a couple of method calls away.

    double avg = Arrays.stream(item.getGrades()).average().getAsDouble();
    
  • Use another for loop and keep a running total of the elements. Don't forget to manually coerce your numerator to a double or you'll run into integer division.

    int sum = 0;
    for(int i : item.getGrades()) {
        sum += i;
    }
    double avg = (sum * 1.0) / item.getGrades().length;
    

In practice, never hard-code your index locations. You can always get the length of an array using .length.

Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
0

You can just use this function, in which would iterate through the array, by dynamically iterating through for loop, we compute sum. Then average would be divided by the length of array.

double getAverage(double[] data)
{
    double sum = 0.0;
    for(double a : data)
        sum += a;
    return sum/data.length;
}
sai
  • 434
  • 5
  • 13
  • If you're going to write a code-only solution, it's preferable to explain what the code's doing so that the OP has some inkling of what the solution means. – Makoto Apr 23 '17 at 23:52
  • @Makoto it was quite self explanatory. – sai Apr 23 '17 at 23:55
  • I don't disagree but that doesn't mean that it's just OK to leave only code around. It's the difference between giving them a fish and teaching them to fish. – Makoto Apr 24 '17 at 00:05
  • True. Updated my answer. – sai Apr 24 '17 at 00:13