Other 2005 FR Questions FR other years Be Prepared Home
A-4
Part (a)
  // returns the average (artithmetic mean) of the values in scores
  // whose subscripts are between first and last inclusive
  // precondition:  0 <= first <= last < scores.length
  private double average(int first, int last)
  {
    double sum = 0; 1
    for (int k = first; k <= last; k++)
      sum += scores[k];
    return sum / (last - first + 1);
  }
Notes:
  1. sum could be an int, but then you'd need a cast in the return statement:
        return (double)sum / (last - first + 1);

Part (b)
  // returns true if each successive value in scores is greater
  // than or equal to the previous value;
  // otherwise, returns false
  private boolean hasImproved()
  {
    for (int k = 1; k < scores.length; k++)
      if (scores[k] < scores[k-1])
        return false;
    return true;
  }

Part (c)
  // if the values in scores have improved, returns the average
  // of the elements in scores with indexes greater than or equal
  // to scores.length/2;
  // otherwise, returns the average of all of the values in scores
  public double finalAverage()
  {
    if (!hasImproved())
      return average(0, scores.length - 1);
    else
      return average(scores.length/2, scores.length - 1);
  }

Other 2005 FR Questions | Back to Contents

Copyright © 2005 by Skylight Publishing
support@skylit.com