2

In school we were asked to create a function that would find the lowest value in a float array. In the first 2 arrays it works. But because the 3rd array only contains negative values they're all lower than the value given at initialization. SO it remains 0.0.

Can anyone help?

float[] numbers = {3.3, 4.5, 7.2, 5.3, 9.0, 2.3}; // min 2.3, max 9.0
float[] numbers2 = {-8.0, 4.5, 1.2, -5.6, -9.1, 4}; // min -9.1, max 4.5
float[] numbers3 = {-0.4, -0.7, -3.5, -1.9, -8.0}; // min -8.0, max -0.4

void setup() {
  float highest = returnHighest(numbers);
  float highest2 = returnHighest(numbers2);
  float highest3 = returnHighest(numbers3);
  println("The highest grade is: " + highest);
  println("The highest grade is: " + highest2);
  println("The highest grade is: " + highest3);
}

float returnHighest(float[] numbers) {
  float highest = 0;

  for(int i = 0; i < numbers.length; i++) {
    if(numbers[i] > highest) {
      highest = numbers[i];
    }
  }

  return highest;
}
1
  • 5
    replace float highest = 0; with float highest = numbers[0]; Commented Oct 4, 2017 at 7:20

5 Answers 5

5

But because the 3rd array only contains negative values they're all lower than the value given at initialization.

The answer is ... somewhat ... simple. You can either initialize your variable lowest with one of the numbers in your array (1), or you can initialize it with the lowest possible float value (2).

(1) Initializing your variable with a number in your array:

float highest = numbers[0];

With this solution, you first have to make sure to not have an empty array. Otherwise you would get an ArrayIndexOutOfBoundsException.

(2) Initializing your variable with the lowest possible float value:

float highest = Float.NEGATIVE_INFINITY;

Every float value is greater than Float.NEGATIVE_INFINITY (well ... except the negative infinity value itself). After your loop you can check whether highest still equals Float.NEGATIVE_INFINITY. In that case the array was empty or contained only negative infinities.

I would recommend approach (2). This is a common pattern for finding the lowest/highest value of a collection/array, and also deals well with empty collections/arrays.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that you set highest to 0 before the loop. Then your if statement never evaluate to true since all values in the array are less than 0. Add a boolean like this:

float returnHighest(float[] numbers) {
  boolean firstTime = true;
  float highest = 0;

  for(int i = 0; i < numbers.length; i++) {
    if(firstTime || numbers[i] > highest) {
      highest = numbers[i];
      firstTime = false;
    }
  }
  return highest;
}

Comments

0

I would suggest setting the first element as the highest value when initialising so your function becomes.

float returnHighest(float[] numbers) {
  float highest = numbers[0];

  for(int i = 0; i < numbers.length; i++) {
    if(numbers[i] > highest) {
      highest = numbers[i];
    }
  }

  return highest;
}

Also I am assuming you made a mistake in the title and want to find the highest value

Comments

0

Have you tried only comparing numbers within the array? Try setting the initial value of highest to the initial value of the array, instead of 0.

Comments

0

You can set the initial value of highest to the first element of array instead of 0:

highest = numbers[0];

but the problem in this way is it will not work when you have empty array.

Also you can solve this problem in more efficient way:

Set the initial value of highest to the lowest value of float number in java

highest = -Float.MAX_VALUE;

in this way it will work fine with any subset of negative float numbers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.