Results 1 to 13 of 13
  1. #1
    micolord is offline Member
    Join Date
    Sep 2011
    Location
    Philippines
    Posts
    41
    Rep Power
    0

    Default Getting/Printing the Highest to lowest

    Printing the highest to lowest inputted numbers.

    int house = {0,0,0,0,0},ctr;
    Scanner Input = new Scanner(System.in);

    for(ctr=0;ctr<=5;ctr++){
    System.out.println("Enter a number: ");
    house[1] = Input.nextInt();

    System.out.println("Enter a number: ");
    house[2] = Input.nextInt();

    System.out.println("Enter a number: ");
    house[3] = Input.nextInt();

    System.out.println("Enter a number: ");
    house[4] = Input.nextInt();
    }

    and now I only need to do is to arrange them and print them from highest to lowest, I've tried some if and else condition and I almost reached 80 lines using them and it didn't worked... My friend told me that there should be three loops, nested loops, loops with condition and a loop to continue on and I don't get it, I'm totally confused.

    Out put should be like this:
    5 - highest inputted number
    4
    3
    2
    1 - lowest inputted

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Getting/Printing the Highest to lowest

    How would you do this by hand, without a computer? Are you allowed to use the Collections class?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    micolord is offline Member
    Join Date
    Sep 2011
    Location
    Philippines
    Posts
    41
    Rep Power
    0

    Default Re: Getting/Printing the Highest to lowest

    Yipee! Here what I've done so far:


    Java Code:
    import java.util.Arrays;
    import java.util.*;
    public class wew
    {
    public static void main(String [] args)
    {
    int[] muffin = new int[] {0,0,0,0,0};
    int ctr;
    Scanner Input = new Scanner(System.in);
    
    for(ctr=0;ctr<5;ctr++){
    if(ctr==0){
    System.out.println("Enter a number: ");
    muffin[0] = Input.nextInt();
    }
    if(ctr==1){
    System.out.println("Enter a number: ");
    muffin[1] = Input.nextInt();
    }
    if(ctr==2){
    System.out.println("Enter a number: ");
    muffin[2] = Input.nextInt();
    }
    if(ctr==3){
    System.out.println("Enter a number: ");
    muffin[3] = Input.nextInt();
    }
    if(ctr==4){
    System.out.println("Enter a number: ");
    muffin[4] = Input.nextInt();
    }
    }
    Arrays.sort(muffin);
    System.out.println(Arrays.toString(muffin));
    }
    }


    The output will be, lowest to highest but I want is it to be highest to lowest, how do I do that???
    Last edited by Norm; 09-14-2011 at 03:38 PM. Reason: added code tags

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Getting/Printing the Highest to lowest

    I want is it to be highest to lowest, how do I do that?
    You could write your own sort.

    Look at your current code.
    Do you see a pattern in the way the value of ctr and the index to the array are the same for all the if statements.
    Try using the for loop variable for the index instead of hardcoding a numeric literal for the index.

    What if you wanted to change the number of numbers to input?
    Last edited by Norm; 09-14-2011 at 03:42 PM.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Getting/Printing the Highest to lowest

    Arrays contains a sort() method that also takes a Comparator as an argument. But somehow I think that using that method is defeating the purpose of your homework assignment.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    micolord is offline Member
    Join Date
    Sep 2011
    Location
    Philippines
    Posts
    41
    Rep Power
    0

    Default Re: Getting/Printing the Highest to lowest

    Quote Originally Posted by KevinWorkman View Post
    your homework assignment.
    Actually it isn't, I have failed to finish this during our hands and now doing it just for a little bit of fun and also to widen my knowledge.

    Quote Originally Posted by Norm View Post
    Try using the for loop variable for the index instead of hardcoding a numeric literal for the index.
    I don't get it ):

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Getting/Printing the Highest to lowest

    For example:
    Java Code:
    if(ctr==4){
        System.out.println("Enter a number: ");
        muffin[4] = Input.nextInt()
    Here ctr and [4] have the same value.
    [4] is a hardcoded numeric literal
    Try replacing the hardcoded numeric literal with a variable:
    muffin[ctr] = ...

  8. #8
    micolord is offline Member
    Join Date
    Sep 2011
    Location
    Philippines
    Posts
    41
    Rep Power
    0

    Default Re: Getting/Printing the Highest to lowest

    Quote Originally Posted by Norm View Post
    For example:
    Java Code:
    if(ctr==4){
        System.out.println("Enter a number: ");
        muffin[4] = Input.nextInt()
    Here ctr and [4] have the same value.
    [4] is a hardcoded numeric literal
    Try replacing the hardcoded numeric literal with a variable:
    muffin[ctr] = ...
    Thanks.

    By the way, I'll have to add an another loop with some if statement??? and will have to remove this Arrays.sort(muffin);
    System.out.println(Arrays.toString(muffin));
    ??? I'm so confused XD

  9. #9
    micolord is offline Member
    Join Date
    Sep 2011
    Location
    Philippines
    Posts
    41
    Rep Power
    0

    Default Re: Getting/Printing the Highest to lowest

    Any reply XDDDDD?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Getting/Printing the Highest to lowest

    You will need to use a loop and a couple of indexes into your array.
    Set one index to the first element in the array. Use the other index to go thru all the remaining elements.
    Compare the value of the element at the first index to the element at the second index.
    If the second element is greater than the first element, swap them.
    Continue moving the second index to the last element in the array.
    You now have the largest element in the array at the location the first index points to.
    Advance the first index to the next slot in the array and do it again.
    When you are at the end, the second largest element will be in the slot where the first index points.
    Continue until there are no more elements to check.

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Getting/Printing the Highest to lowest

    Quote Originally Posted by micolord View Post
    Any reply XDDDDD?
    Just a word to the wise- bumping your thread, especially after only a short time has gone by, actually hurts your chances of getting help. It makes you look impatient- there are thousands of posts here, each with its own urgent user, and their time is just as valuable as yours. It also makes it look like your thread has more replies, which many people will take to mean you've already received help, so they won't even read your post.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    micolord is offline Member
    Join Date
    Sep 2011
    Location
    Philippines
    Posts
    41
    Rep Power
    0

    Default Re: Getting/Printing the Highest to lowest

    Quote Originally Posted by KevinWorkman View Post
    Just a word to the wise- bumping your thread, especially after only a short time has gone by, actually hurts your chances of getting help. It makes you look impatient- there are thousands of posts here, each with its own urgent user, and their time is just as valuable as yours. It also makes it look like your thread has more replies, which many people will take to mean you've already received help, so they won't even read your post.
    Yes I'm badly sorry for being impatient, I'm just a little be tired that's all, sorry - again.

  13. #13
    micolord is offline Member
    Join Date
    Sep 2011
    Location
    Philippines
    Posts
    41
    Rep Power
    0

    Default Re: Getting/Printing the Highest to lowest

    Quote Originally Posted by Norm View Post
    You will need to use a loop and a couple of indexes into your array.
    Set one index to the first element in the array. Use the other index to go thru all the remaining elements.
    Compare the value of the element at the first index to the element at the second index.
    If the second element is greater than the first element, swap them.
    Continue moving the second index to the last element in the array.
    You now have the largest element in the array at the location the first index points to.
    Advance the first index to the next slot in the array and do it again.
    When you are at the end, the second largest element will be in the slot where the first index points.
    Continue until there are no more elements to check.
    Thank you so much.

Similar Threads

  1. Replies: 4
    Last Post: 02-15-2011, 11:05 PM
  2. Need Lowest and Highest decimal point for 0.73 and 0.76
    By maran.ramar in forum New To Java
    Replies: 5
    Last Post: 11-15-2010, 01:58 AM
  3. cant get highest and lowest to display
    By tracey in forum New To Java
    Replies: 2
    Last Post: 05-16-2010, 08:48 AM
  4. QuickSort highest lowest Situation
    By Tenn in forum New To Java
    Replies: 17
    Last Post: 05-06-2009, 04:37 AM
  5. Replies: 21
    Last Post: 11-05-2008, 02:31 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
  •