Results 1 to 9 of 9
  1. #1
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default Return smallest value

    Hi I have an error with my code but I do not know what it is, here are my codes:

    Java Code:
    public class Q2 {
    
        public static void main(String[] args) {
    
        	int[] numbers={1,2,4,5,10,100,2,-22};
    
        	int mostSmall= find(numbers);
    
        	System.out.println(mostSmall);
        }
    
        public static int find(int[] numbers){
    
    		int smallest=0;
    
    		for(int i=0;i<numbers.length;i++){
    
    		if(numbers[i]<=numbers[i+1]){
    
    			smallest= numbers[i];
    		}
    
    		else{
    
    			smallest= numbers[i+1];
    
    		}
    
    		}
    			return smallest;
    		}
    
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by jaylimix View Post
    Hi I have an error with my code but I do not know what it is, here are my codes:

    Java Code:
        public static int find(int[] numbers){
    
    		for(int i=0;i<numbers.length;i++){
    
    		if(numbers[i]<=numbers[i+1]){
    Most readers here aren't psychic; I am so I knew an ArrayIndexOutOfBounds error was thrown. Suppose your array length is 10; variable i goes from 0 to 9 (inclusive) but you are trying to access numbers[9+1] which goes kaboom. You have to fix that. b.t.w. you don't have to compare two consecutive numbers in the array to find the smallest number in the array.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default



    Java Code:
    import java.math.*;
    
    public class Q2 {
    
        public static void main(String[] args) {
    
        	int[] numbers={1,2,4,5,10,100,2,-22};
    
        	int mostSmall= find(numbers);
    
        	System.out.println(mostSmall);
        }
    
        public static int find(int[] numbers){
    
    		int smallest=0;
    
    		for(int i=0;i<numbers.length;i++){
    
    		smallest= Math.min(numbers[7], numbers[i]);
    
    		}
    
    		return smallest;
        }
    }

  4. #4
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Thanks for the tips.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by jaylimix View Post
    Java Code:
    		smallest= Math.min(numbers[7], numbers[i]);
    What is this supposed to mean?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    It means comparing all the numbers in the array with the last number of the array to get the smallest value. Why do you ask?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by jaylimix View Post
    It means comparing all the numbers in the array with the last number of the array to get the smallest value. Why do you ask?
    So your array always contains eight numbers? I would've picked the first number of the array instead (if there is at least one number in the array, otherwise I have to think a bit).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    jaylimix is offline Member
    Join Date
    May 2011
    Posts
    29
    Rep Power
    0

    Default

    Well based on my question paper(college), yeah this particular question contains only eight numbers. But I get what you mean :-)

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    The problem with your coding style is that the code breaks as soon as there are not 8 elements in the array.
    Would: int lastIndex = numbers.length-1;
    be more likely to always be right.
    Plus the variable name shows what you intend.

Similar Threads

  1. smallest number java program
    By elmo in forum New To Java
    Replies: 9
    Last Post: 11-18-2010, 10:29 PM
  2. Array[] get smallest/largest value
    By bobocheez in forum New To Java
    Replies: 13
    Last Post: 09-10-2010, 12:16 AM
  3. Return Smallest Index
    By Gamerpro in forum New To Java
    Replies: 4
    Last Post: 02-25-2009, 03:31 AM
  4. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 11:30 PM
  5. Getting smallest possible Double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 03:15 PM

Posting Permissions

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