Results 1 to 4 of 4
  1. #1
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default Checking ascending order of array

    Hi got an array form main method which is entered by the user (array size not known until input by user) and then theres a method which returns a boolean of true or false if the array entered is in ascending order. I can get the user input fine, just having trouble with the logic of the method to check if the arrays in ascending order. Heres my code:

    public static boolean numerical(int[] a){

    boolean result = false;

    for(int i=0;i<a.length-1;i++){
    if(a[i] < a[i+1]){
    result = true;
    }
    else if(a[i] > a[i]+1){
    result = false;
    }
    if(result != true){
    return result;
    }
    }


    return result;


    }

    I thought this would work but I get not what I wanted when I run the code. I thought that we can check the array contents in order from i against the number nearest to it i+1 then if the current i in the array is > then the contents of i+1 it would return the boolean false, as it shows the arrays not in order. Could someone help with the logic of this or help with my code.

    This is a homework exercise so I have to write the searching by myself without like already built array methods.

    Thanks in advance

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

    Default

    You have it almost right; an array is in ascending or if a[i] <= a[i+1] for every i in the range 0 ... n-2; note the 'n-2' because when i is at its upperbound you also refer to element i+1 which is n-2+1 which is the last element in the array (arrays are zero indexed). If you find an element for which the above condition doesn't hold you can return false immediately.

    kind regards,

    Jos

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Java Code:
    else if(a[i] > [B]a[i]+1[/B])

  4. #4
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default

    else if(a[i] > a[i]+1)

    Thanks I changed it to :
    else if(a[i] > a[i+1])

    and it started to work.
    Thanks for all the help

Similar Threads

  1. Replies: 5
    Last Post: 03-19-2010, 12:26 AM
  2. Spiral Array Order Number
    By rendra in forum Advanced Java
    Replies: 14
    Last Post: 11-20-2009, 03:20 AM
  3. sorting variable values in ascending order?
    By jeffrey in forum New To Java
    Replies: 0
    Last Post: 07-14-2009, 07:59 AM
  4. How to sort array objects into alphabetical order...
    By lisalala in forum New To Java
    Replies: 5
    Last Post: 03-03-2009, 11:01 AM
  5. How to add coins in ascending order in arraylist
    By tribujohn in forum New To Java
    Replies: 2
    Last Post: 01-23-2009, 04:31 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
  •