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 5 of 5

Thread: Number Array

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Number Array

    So I have an array of values:
    dht = new int[] { 41, 68, 77, 74, 74, 68, 88 };

    And I want to return the index/of the position of the highest number. In this case 88.
    I understand it is in the 6th position.

    So,
    public static int hottestDay( int[] temp) {
    		int hottest = 88;
    		for(int i; i < dht.length ; i++){
    			if( dht[i] == hottest)
    				return i;
    		}

    I am more interested in how you solve, than the actual answer itself. If someone could get on AIM and walk me through this and other stuff, I would be so happy.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Number Array

            int[] test = {1, 2, 3, 4};
            int highestVal = 0;
            int position = -1;
            for (int i = 0; i < test.length; i++) {
                if (test[i] > highestVal) {
                    highestVal = test[i];
                    position = i; //Kinda tells position of highest val 
                }
     
            }
            if (highestVal == 0 || position == -1) {
                System.out.println("Test Failed?");
            } else {
                System.out.println("Highest Array Element is " + highestVal);
            }

    That might be what you're kind of after? It stores highest value so far and its position.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Number Array

    Kinda of.
    int[] test = {1, 2, 3, 4};
            int highestVal = 0;
     
     
            int position = -1;
            for (int i = 0; i < test.length; i++) {
                if (test[i] > highestVal) {
                    highestVal = test[i];
                    position = i; //Kinda tells position of highest val
                }
     
            }
            if (highestVal == 0 || position == -1) {
                System.out.println("Test Failed?");
            } else {
                System.out.println("Highest Array Element is " + highestVal);
            }
     
    return position;
    }

    That would work, but what if
    int[] test = {-5,-4,-3,-2,-1};

    It'll return position -1.
    However, the highest value would be -1, which is at position 4.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Number Array

    Change newbie's code to set it to set highest value to the first index of the array. Then compare it like before. This way, it can handle negatives.
    Last edited by javapenguin; January 15th, 2011 at 04:25 PM.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Number Array

    indices != values

    newbie's code almost works. The only case it won't work is if the highest value happens to be 0. There's no need to store the highest value and the position. Furthermore, except for an array of size 0 there is always guaranteed to be at least one value which is the largest (or in this case, most positive). How you handle multiple highest values in the list is up to you, though usually you would just return the first one you find.

    public static int max(int[] numbers)
    {
        if(numbers.length == 0)
        {
            return -1; // no numbers to find the max of
         }
        int highestIndex = 0;
        for(int i = 1; i < numbers.length; ++i)
        {
            if(numbers[i] > numbers[highestIndex)
            {
                highestIndex = i;
            }
        }
        return highestIndex;
    }

    Test cases:
    System.out.println(max(new int[]{1, 2, 3, 4})); // prints 3
    System.out.println(max(new int[]{})); // prints -1
    System.out.println(max(new int[]{-1, 0})); // prints 1
    System.out.println(max(new int[]{-1, -3, -6 ,-1, 3, 100, 50})); // prints 5
    Last edited by helloworld922; January 17th, 2011 at 05:02 AM.

Similar Threads

  1. Replies: 1
    Last Post: December 4th, 2010, 05:26 PM
  2. Finding the highest number in an array?
    By halfwaygone in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 17th, 2010, 03:56 PM
  3. letter to number
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2009, 07:01 AM
  4. Help With Odd/Even number program
    By JonoScho in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 23rd, 2009, 10:53 AM
  5. Reverse Number
    By java1 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 28th, 2009, 10:19 AM