lundi 4 octobre 2021

What am I doing wrong - arrays - finding highest value element in array

I am working on creating a method to find the highest value element in an array. I did everything it explains in the book(set the value of maximumValue to subscript 0, compared each value starting with subscript 1 using a for loop with a nested if statement setting any values higher than the last to the maximumValue variable). I've tried testing it using the example I have in the comments but for some reason, I keep getting back 5.0 instead of 9.0. I reread it several times and worked it out in my head and I'm sure there is something I must be missing but from what I can tell, it should be giving me 9.0 for that example. Please point me in the right direction if there is some glaring mistake, or let me know I'm not crazy. Thanks in advance!

/**
 * Purpose: find the highest element in an array
 * Signature: input array, returns double
 * Examples:
 * findMaximum({6, 4, 9, 5}) return 9
 * findMaximum({1.5, -3, 6, 2}) return 6
*/
public static double findMaximum(double[] numbers)
{
    double maximumValue = numbers[0];
    
    for (int index = 1; index < numbers.length; index++)
    {
        if (numbers[index] > maximumValue);
        maximumValue = numbers[index];
    }
    
    return maximumValue;
}

Aucun commentaire:

Enregistrer un commentaire