Results 1 to 4 of 4
  1. #1
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Question Using Arrays to Find Min and Max

    Hello fellow Programmers,

    Right now, I am making an array and making a loop that finds the minimum and maximum numbers in the array. For some reason, the output does not find the min and max; instead, it prints out the first number in the array. Please guide me so I can solve this problem and get the code working.

    Thanks

    - SimeonTG

    Code:
    Java Code:
    public class MinAndMax {
    
    	public static void main(String[] args) {
    		int nums[] = new int[5];
    		int min, max;
    		
    		  nums[0] = 99;     
    		  nums[1] = -10;     
    		  nums[2] = 100123;     
    		  nums[3] = 18;     
    		  nums[4] = -978;  
    		  
    		  min = max = nums[0];
    		  for (int i = 1; i > 5; i++) {
    			  if (nums[i] < min) min = nums[i];
    			  if (nums[i] > max) max = nums[i];
    		  }
    		  
    		  System.out.println("min and max: " + min + " " + max); 
    		
    
    	}
    Output:

    Java Code:
    min and max: 99 99

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Using Arrays to Find Min and Max

    The for loop never starts looping because the condition is not met. Therefore the values stick with the definition in line 13.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Using Arrays to Find Min and Max

    If you want to make it easy on yourself for testing, do the following.

    Java Code:
    int [] nums = {99,-10, 100123, 18, -978};
    Then just iterate over the array using the enhanced for syntax.

    Java Code:
    for (int v : nums) {
       // do something with v.
    }
    Note: if you want to alter the array then you must use the traditional loop.

    Edit: I thought about this after I posted and in your specific algorithm, a regular
    loop is probably better since you can assign the first value of the array to min and max,
    just as you have done.

    Regards,
    Jim
    Last edited by jim829; 11-20-2015 at 02:57 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Default Re: Using Arrays to Find Min and Max

    Quote Originally Posted by benji2505 View Post
    The for loop never starts looping because the condition is not met. Therefore the values stick with the definition in line 13.
    Oh yeah! Thanks dude. It got the problem solved.

Similar Threads

  1. Cannot find or load or find main class
    By cornelombaard in forum New To Java
    Replies: 3
    Last Post: 11-19-2014, 03:04 PM
  2. Replies: 4
    Last Post: 06-28-2014, 05:00 PM
  3. Inputting arrays to find the average
    By DuncanHollows in forum New To Java
    Replies: 19
    Last Post: 07-28-2011, 06:55 PM
  4. Replies: 8
    Last Post: 02-08-2011, 07:27 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •