Someone please explain this question, I'm quite confused (isAscending)

complete the function isAscending that when passed an array of floating-point numbers, returns true if the array is sorted in ascending order (such that each item is less than or equal to the next item), and false otherwise.

boolean isAscending(float[] a) {
  ...
}

Answers

  • edited June 2016
    // forum.Processing.org/two/discussion/17239/
    // someone-please-explain-this-question-i-m-quite-confused
    
    // GoToLoop (2016-Jun-21)
    
    @ SafeVarargs static final boolean isAscending(final float... vals) {
      if (vals == null || vals.length == 0)  return false;
      float prev = MIN_FLOAT;
      for (final float curr : vals)  if (prev > (prev = curr))  return false;
      return true;
    }
    
    void setup() {
      println(isAscending(-3.95, -3.94, 1.99, 1.9901, 1.999)); // true
      println(isAscending(QUARTER_PI, THIRD_PI, HALF_PI, PI, TAU)); // true
      println(isAscending(.25)); // true
      println(isAscending()); // false
      println(isAscending(null)); // false
      println(isAscending(1e3, EPSILON)); // false
      println(isAscending(1e-3, 1e3)); // true
      println(isAscending(1e3, 1e3)); // true
      exit();
    }
    
  • sigh. complete answers to homework questions.

    complete answers to homework questions WHEN SHE ONLY ASKED FOR CLARIFICATION.

  • Answer ✓

    marissa,

    i think you are expected to write code, using the supplied method signature, that tests to make sure the supplied list of number is in ascending order, ie that the second is bigger than the first, the third is bigger than the second etc.

    you need a loop and a test. if a number is smaller (or equal) than the one before then you can return false immediately. if you reach the end of the list you can return true.

  • Thank you!! this really helped :)

Sign In or Register to comment.