Results 1 to 10 of 10
  1. #1
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default Finding maximum number at index position

    Hi, I've created a program and it seems to find the maximum value when a number of command line arguments are entered but i cannot get it to also print the index of which value it is:

    for (int i=0; i < args.length; i++)
    {
    double aValue = Double.parseDouble(args[i]);

    if (aValue > max)
    {
    max = aValue;
    largestIndex = i;

    }

    largestIndex = i;

    }

    //System.out.println(max + largestIndex);

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    That's because you're adding them numerically.

    System.out.println(2 + 5); // prints 7
    System.out.println(2 + "" + 5); // prints 25
    System.out.println(2 + " and " + 5); // prints 2 and 5

  3. #3
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    No i've just put it like that jsut to test, but the actual print output is:

    System.out.println("The maximum value is " + max + " which is on index " + largestIndex);

    I'm presuming it still wouldnt make a difference.

    I keep getting the error that largestIndex might not be initialized.

  4. #4
    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

    ... and that second 'largestIndex = i;' should go.

    kind regards,

    Jos

  5. #5
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    what about that. Where would it go? I do need that dont i, because then the value wont be assigned to largestIndex because i is in the loop.

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    initialise largestIndex to 0 before the loop

  7. #7
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    It works, but it outputs the wrong index value..

  8. #8
    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 Shyamz1 View Post
    what about that. Where would it go? I do need that dont i, because then the value wont be assigned to largestIndex because i is in the loop.
    It should go to the bit bucket because you don't need it nor do you want it. If you want to find the largest number in an array keep track of the index of the largest number so far. You can set the index at, say, -1 if you haven't found any number yet. Otherwise compare a current number with array[index] where index is the index of the largest number in the array so far and update it if necessary. In code it translates to this:

    Java Code:
    int maxIndex(double[] array) {
       int index= -1;
       if (array != null)
          for (int i= 0; i < array.length; i++)
             if (index == -1 || array[i] > array[index])
                index= i;
       return index;
    }
    kind regards,

    Jos

  9. #9
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    I have yet learnt about array. I've only learnt about args so far.

  10. #10
    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 Shyamz1 View Post
    I have yet learnt about array. I've only learnt about args so far.
    Which is also an array ... an array of String to be exact ;-)

    kind regards,

    Jos

Similar Threads

  1. Finding nth prime number
    By dextr in forum New To Java
    Replies: 2
    Last Post: 04-13-2010, 12:42 AM
  2. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 01:37 AM
  3. Replies: 1
    Last Post: 02-05-2010, 11:41 AM
  4. Find index position of every word in a String
    By pentace in forum New To Java
    Replies: 6
    Last Post: 06-28-2009, 09:26 PM
  5. finding length on a number
    By thekrazykid in forum New To Java
    Replies: 8
    Last Post: 12-12-2008, 09:07 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
  •