Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Find the lowest number

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.
    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
    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


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default 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.

  3. The Following User Says Thank You to Parranoia For This Useful Post:

    Elyril (April 13th, 2014)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Elyril (April 13th, 2014)

Similar Threads

  1. Finding the lowest number in an array.
    By EatMyBible in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2013, 02:49 AM
  2. [SOLVED] flightpaths, finding lowest cost -- lowest amount of crossovers
    By CjStaal in forum Algorithms & Recursion
    Replies: 4
    Last Post: May 8th, 2012, 12:47 AM
  3. Replies: 1
    Last Post: December 4th, 2010, 05:26 PM
  4. Loop to note position of lowest number
    By fortune2k in forum Loops & Control Statements
    Replies: 3
    Last Post: November 23rd, 2010, 10:05 AM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM