Home > Blockchain >  Returning largest negative number in 2D array
Returning largest negative number in 2D array

Time:02-26

//main 
public class 2d {
    public static void main(String[] args) {

    double[][] temperatures = {
        {-5.5, -6.7, -3, -8.7, -13.3, -7.1, -6.5} ,
        {-4.5, -19.7, -4, -8.7, -13.3, -21.9, -6.5},
        {-5.5, -6.7, -3, -8.7, -13.3, -7.1, -6.5}
    };

    System.out.println(temps(temperatures));
}  

//method
public static double temps(double[][] temperatures){

    double highestTemp = 0.0;
    for(int i=0; i<temperatures.length; i  ){
        for(int j=0; j<temperatures[i].length; j  ){
            if(temperatures[i][j] > highestTemp ){
                highestTemp = temperatures[i][j]; 
            }else if(highestTemp > temperatures[i][j]){
                highestTemp = temperatures[i][j];
            }
    }
}
    return highestTemp;

    }
}

The correct output is suppose to return -3 since the largest number in the 2d array is -3, but my code returns -6.5 anyone know how to fix this?

CodePudding user response:

Your code checks for highest and for lowest value, but always assigns to highestTemp. The else if branch should assign to a lowestTemp if you need that, or you should remove it completely. You also need to initialize highestTemp with a different value, you need to put a really low number in there to start, so that the real data is actually larger.

public static double temps(double[][] temperatures) {
    double highestTemp = -Double.MAX_VALUE;
    for (int i = 0; i < temperatures.length; i  ){
        for (int j = 0; j < temperatures[i].length; j  ){
            if (temperatures[i][j] > highestTemp) {
                highestTemp = temperatures[i][j]; 
            }
        }
    }
    return highestTemp;
}

I am using -Double.MAX_VALUE here cause Double.MIN_VALUE is positive.

CodePudding user response:

Just switch your method with this.

public static double temps(double[][] temperatures) {
    double highestTemp = temperatures[0][0];
    
    for(int i = 0; i < temperatures.length; i  ){
        for(int j = 0; j < temperatures[i].length; j  ) {
            if(temperatures[i][j] > highestTemp ) {
                highestTemp = temperatures[i][j]; 
            }
        }
    }
    
    return highestTemp;

    }
}

CodePudding user response:

Your code won't work since you initialize to zero and you have a bunch of negative numbers. Doing it this way you don't need to worry about an initialization value.

Double max = null;
for (double[] temps : temperatures) {
    double localMax = Arrays.stream(temps).max().getAsDouble();
    if (max == null) {
        max = localMax;
    } else {
        max = Math.max(max, localMax);
    }
}
        
System.out.println(max);

CodePudding user response:

The problem is here. Look closely at these statements.

           if(temperatures[i][j] > highestTemp ){
                highestTemp = temperatures[i][j]; 
            }else if(highestTemp > temperatures[i][j]){
                highestTemp = temperatures[i][j];
            }

You don't need both conditionals. Just do it like this.

        double highestTemp = -Double.MAX_VALUE;
        for (int i = 0; i < temperatures.length; i  ) {
            for (int j = 0; j < temperatures[i].length; j  ) {
                if (temperatures[i][j] > highestTemp) {
                    highestTemp = temperatures[i][j];
                }
            }
        }

An alternative would be to use streams and simplify the process.

Here is the method

public static double temps(double[][] temperatures) {
    return Arrays.stream(temperatures)
                .flatMapToDouble(Arrays::stream)
                .max()
                .orElseThrow(()->new IllegalArgumentException("Empty array"));  
        
}

If you want to return the lowest and highest you can do it like this. First defined a record which is an immutable class.

record Temps(double getLow, double getHigh){}

Then create them stream as before but use summaryStatistics. When done populate the class using the constructor and return the instance.

public static Temps temps(double[][] temperatures) {
    DoubleSummaryStatistics dss =  Arrays.stream(temperatures)          
                .flatMapToDouble(Arrays::stream).summaryStatistics();
    return new Temps(dss.getMin(), dss.getMax());
}

With your current data:

Temps temp = temps(temperatures);
                
System.out.println(temp.getLow());
System.out.println(temp.getHigh());

prints

-21.9
-3.0
  •  Tags:  
  • java
  • Related