# Find the lowest number

• April 12th, 2014, 11:28 PM
Elyril
Find the lowest number
I have to find the average, highest, and lowest numbers in an array. I already have the average and highest, and I thought I could find the lowest number the same way I found the highest, but it's not working. It just keeps coming out to 0. Please help, I'm pretty confused and I've tried out different ways, but I want to see if there are better ways than doing MAX_VALUE for the lowest, then looping it through.
Code Java:

```import java.util.Scanner;   public class Test_Scores { public static void main(String[] args) { //array,scanner,values Scanner input = new Scanner(System.in); double[] allScores = new double[5]; double sum = 0; double average = 0; double highest = allScores[0]; double lowest = allScores[0];   for(int i = 0; i < allScores.length; i++) { System.out.print("Enter score for test " + (i + 1) + ": "); allScores[i] = input.nextDouble(); } //Average grade for(int a = 0; a < allScores.length; a++) { sum += allScores[a]; average = sum / 5; } //Highest grade for(int h = 0; h < allScores.length; h++) { if (allScores[h] > highest) { highest = allScores[h]; } } //Lowest grade for(int l = 5; l < allScores.length; l--) { if (allScores[l] < lowest) { lowest = allScores[l]; } } //output System.out.println("The average grade is: " + average); System.out.println("The highest grade is: " + highest); System.out.println("The lowest grade is: " + lowest); } }```
These are the results
Code Java:

```Enter score for test 1: 98 Enter score for test 2: 96 Enter score for test 3: 92 Enter score for test 4: 65 Enter score for test 5: 85   The average grade is: 87.2 The highest grade is: 98.0 The lowest grade is: 0.0```
• April 13th, 2014, 01:56 AM
Parranoia
Re: Find the lowest number
Well, initially your lowest score is set to 0. Unless you enter a number less than 0, your for loop will not find anything "lower"

Setting the highest and lowest scores should be done after you have taken the input from the user.
• April 13th, 2014, 06:32 AM
Norm
Re: Find the lowest number
When you are using a loop to search an array for highest or lowest, One technique is to take the first one in the array for the initial value and to compare all the others in the array against it, changing the value when found.