user3554678
user3554678

Reputation: 5

Trying to find minimum value in an array and return it to main

Coding for a game show where 4 players guess a price of an item. Exact price is user inputted first. The way I'm setting it up to find the minimum difference between player guess price and exact price of item. The player with the minimum difference is the round winner.

public static int getRoundsWon(double [] guessPrice, double exactPrice) { 
    double minValue = 0;
        do {
            try {
                for (int x = 0; x < 5; x++) {
                    minValue = (guessPrice[x] - exactPrice);
                }
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "invalid");
            }
        } while (numRounds <=3)

I want to use linear search but am not sure how to. Would I do Math.min(minValue) ?

Upvotes: 0

Views: 116

Answers (3)

usddddd
usddddd

Reputation: 66

I think what you'd want to do in this situation is store the difference between the players guess and the exact price in a temp variable in the loop body and then check if that value is less than the current minimum, in which case you would replace the minimum with the temp value. I would use a sentinel as the initial value of minimum, something like Integer.Max_VALUE so that the player's guesses would always be less than the initial value of minimum.

// set minValue to largest possible value of double
double minValue = Double.MAX_VALUE;
for(int x = 0; x < 4; x++ ){
    // set temp to difference between user's guess and exact price
    double temp = Math.abs(exactValue - userGuess);
    // set minValue to minimum of previous minValue or userGuess
    minValue = Math.min(minValue, temp);
}

Upvotes: 0

Matias Cicero
Matias Cicero

Reputation: 26331

As you suggested, i am using a linear search, and setting the initial minimum value to the maximum value of a double, making sure that it will be replaced by the first guess price. I am also returning a winner index of -1 if the array of guess prices is empty, letting you handle that error (instead of throwing or catching exceptions)

int winnerIndex = -1;
double minDifference = Double.MAX_VALUE;

for(int i = 1; i < guessPrices.length; i++) {
   double temp = guessPrices[i] - exactPrice;
   if(temp < minDifference) {
        minDifference = temp;
        winnerIndex = i;
   }
}

return winnerIndex;

Upvotes: 0

WoDoSc
WoDoSc

Reputation: 2618

This code will perform a linear search for the (absolute) minimum difference, and it returns the 0-based index (according to the guessPrice array) of the winner. The absolute difference means that the difference can be negative or positive, ma only its absolute value it is considered.

public static int getRoundsWon(double[] guessPrice, double exactPrice) {
        //first set (temporary) the first player as the winner (and its difference as the minimum)
        double minValue = Math.abs(exactPrice-guessPrice[0]);
        int roundWinner = 0;
        for (int k=1;k<guessPrice.length;k++) {  //then check for all other players
            double diff = Math.abs(exactPrice-guessPrice[k]);
            if (diff<minValue) { //if we found a new minimum
                minValue=diff; //store the new minimum
                roundWinner=k; //and set the new temporary winner
            }
        }
        return roundWinner; //return the actual winner
    }

The method requires that the length of the guessPrice array is at least one (i.e. at least one element), but since in your game you said you have four players, this should be not a problem.

Upvotes: 1

Related Questions