Results 1 to 13 of 13
- 09-14-2011, 02:36 PM #1
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
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
- 09-14-2011, 03:26 PM #2
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!
- 09-14-2011, 03:31 PM #3
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
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
- 09-14-2011, 03:39 PM #4
Re: Getting/Printing the Highest to lowest
You could write your own sort.I want is it to be highest to lowest, how do I do that?
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.
- 09-14-2011, 03:42 PM #5
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!
- 09-14-2011, 04:02 PM #6
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
- 09-14-2011, 04:05 PM #7
Re: Getting/Printing the Highest to lowest
For example:
Here ctr and [4] have the same value.Java Code:if(ctr==4){ System.out.println("Enter a number: "); muffin[4] = Input.nextInt()
[4] is a hardcoded numeric literal
Try replacing the hardcoded numeric literal with a variable:
muffin[ctr] = ...
- 09-14-2011, 04:16 PM #8
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
- 09-14-2011, 04:26 PM #9
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
Re: Getting/Printing the Highest to lowest
Any reply XDDDDD?
- 09-14-2011, 04:27 PM #10
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.
- 09-14-2011, 04:38 PM #11
Re: Getting/Printing the Highest to lowest
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!
- 09-14-2011, 04:43 PM #12
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
- 09-14-2011, 04:44 PM #13
Member
- Join Date
- Sep 2011
- Location
- Philippines
- Posts
- 41
- Rep Power
- 0
Similar Threads
-
I need a java program that takes scores and ouputs average, highest, and lowest score
By TheSweetHelloKitty in forum New To JavaReplies: 4Last Post: 02-15-2011, 11:05 PM -
Need Lowest and Highest decimal point for 0.73 and 0.76
By maran.ramar in forum New To JavaReplies: 5Last Post: 11-15-2010, 01:58 AM -
cant get highest and lowest to display
By tracey in forum New To JavaReplies: 2Last Post: 05-16-2010, 08:48 AM -
QuickSort highest lowest Situation
By Tenn in forum New To JavaReplies: 17Last Post: 05-06-2009, 04:37 AM -
[SOLVED] Help with arrays, printing highest and lowest value of the array.
By Sophiie in forum New To JavaReplies: 21Last Post: 11-05-2008, 02:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks