Colin747
Colin747

Reputation: 5013

Return array to method

I have a method to total up average results which calls another method which stores an array of the results but I seem to having trouble returning the array:

Array code:

static double findAverages() {
    double [] averagesArray = new double[10];
    for(int i = 0; i < 9; i++) {
        double total = (studentMarksArray[i][0]+studentMarksArray[i][1]+studentMarksArray[i][2])/3;
        averagesArray[i] = total;
    }
    return averagesArray;
}

Method calling array:

static void highestStudentAvgMark() {
    findAverages();
    double max = averagesArray[0];
    for (int i = 1; i < averagesArray.length; i++) {
        if (averagesArray[i] > max) {
            max = averagesArray[i];
        }
    }
    findMark(max, averagesArray);
    System.out.println(max);
}

Upvotes: 1

Views: 86

Answers (2)

Memran
Memran

Reputation: 405

Your method signature says it returns double, but you are returning double[].

Also `findAverages();' is not stored locally, and so is not used in your second method.

Upvotes: 1

Blub
Blub

Reputation: 3822

You have defined double and not double [] as return type of your method.

Also averagesArray is a local variable in your findAverages() method, so it is not visible in other methods! You need to use the return value of findAverages():

static void highestStudentAvgMark() {
  double[] averagesArray = findAverages();
  double max = averagesArray[0];
  for (int i = 1; i < averagesArray.length; i++) {
    if (averagesArray[i] > max) {
      max = averagesArray[i];
    }
  }
  findMark(max, averagesArray);
  System.out.println(max);
}

Upvotes: 3

Related Questions