Chaite
Chaite

Reputation: 23

Finding the lowest value in the array

int min = temperature[0];
//Here, i have initialized the minium array. 
else if(temperature[i] < min) {
            min = temperature[i];

And here i have compared the values in the array. But as the initialization of min is 0. It is going to minimum value zero all the time. How do i fix it. Here is my whole code.

 int[] temperature = new int[12];
    Scanner kb = new Scanner(System.in);

    int min = temperature[0];
    int max = temperature[0];

    int counter = 0;
    for (int i = 0; i < temperature.length; i++) {
        System.out.println("Please enter the temperature:" + i);
        temperature[i] = kb.nextInt();
        counter += temperature[i];
        if (temperature[i] > max) {
            max = temperature[i];
        }
        else if(temperature[i] < min) {
            min = temperature[i];

        }
    }
    int average = counter / temperature.length;
    System.out.println("Displaying the average temperature:" + average);
    System.out.println("The lowest temperature is:" + min);
    System.out.println("The highest temperaature is:" + max);
    }
}

Upvotes: 1

Views: 331

Answers (2)

Elliott Frisch
Elliott Frisch

Reputation: 201447

Remove the else, logically - if the value is less than the current minimum we want to update current minimum (regardless of the state of the current maximum). We can actually make it clearer using Math.max(int, int) and Math.min(int, int). And, we can't default min and max to initial values without having read the input (unless we use unambiguously absurd values). Like,

int[] temperature = new int[12];
Scanner kb = new Scanner(System.in);

int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, counter = 0;
for (int i = 0; i < temperature.length; i++) {
    System.out.println("Please enter the temperature:" + i);
    temperature[i] = kb.nextInt();
    counter += temperature[i];
    max = Math.max(max, temperature[i]);
    min = Math.min(min, temperature[i]);
}
int average = (int) (counter / (double) temperature.length);
System.out.println("Displaying the average temperature:" + average);
System.out.println("The lowest temperature is:" + min);
System.out.println("The highest temperaature is:" + max);

Otherwise, you need two loops. Finally, note you were using integer math in calculating average. You presumably want floating point math.

Or, even better, use IntSummaryStatistics like

int[] temperature = new int[12];
Scanner kb = new Scanner(System.in);
for (int i = 0; i < temperature.length; i++) {
    System.out.println("Please enter the temperature:" + i);
    temperature[i] = kb.nextInt();
}
IntSummaryStatistics iss = IntStream.of(temperature).summaryStatistics();

System.out.println("Displaying the average temperature:" + iss.getAverage());
System.out.println("The lowest temperature is:" + iss.getMin());
System.out.println("The highest temperaature is:" + iss.getMax());
System.out.println("The total of all values is:" + iss.getSum());

Upvotes: 3

jojois74
jojois74

Reputation: 805

The solution is to initialize min to the first value of your array, if the array had at least one value. You could also set it to Integer.MAX_VALUE if you really want.

Upvotes: 1

Related Questions