May I know how to find the largest number in an array?
Sunshineserene
0
Junior Poster
Recommended Answers
Jump to PostArrays.sort()
Jump to Postalexchen
1. We don't just give people the code for their homework - that teaches them nothing, and
2. I guess you hadn't realised that this is a Java forum?
Jump to PostJust use one pass of the bubble sort algorithm as hinted at in the code in previous post. The larget value in the array will be in the last position of the array after one pass, the second largest in second last position after two passes etc..
Jump to PostStart with a variable = largest negative int.
Loop thru array and if any element is > that value, replace the value with the array element.
At the end of the loop the variable will contain the largest value in the array.
Jump to PostWell don't assign the result of Arrays.sort to anything.
A "senior moment"? Arrays.sort sorts the array you pass to it and has return type null. Hence my concern that it changes the actual source data.
As for nth highest, median etc, I fully agree with you.
All 22 Replies
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
alexchen
-12
Junior Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Sunshineserene
0
Junior Poster
aspire1
36
Light Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
sirlink99
56
Practically a Master Poster
Eric Cute
33
Posting Whiz in Training
Sunshineserene
0
Junior Poster
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
Sunshineserene
0
Junior Poster
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
Sunshineserene
0
Junior Poster
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
Sunshineserene
0
Junior Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
masijade
commented:
I guess I deserved that. ;-)
+10
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Sunshineserene
0
Junior Poster
Sunshineserene
0
Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.