Loading...
Processing Forum
Recent Topics
All Forums
Move this topic
Forum :
Share your Work
Programming Questions
Core Library Questions
Contributed Library Questions
Android Processing
Processing with Other Languages
Integration and Hardware
Library and Tool Development
Events and Opportunities
General Discussion
Sub forum :
Move this topic
Cancel
General Discussion
Other
joe po
How Would I Write a Function That Takes an Array as The Argument and Returns the Second Largest Element
in
General Discussion
•
Other
•
1 year ago
write a function that takes an array as the argument and returns the second largest element ?? just get me in the right direction im kinda lost :S
1
Replies(3)
not_55
Re: How Would I Write a Function That Takes an Array as The Argument and Returns the Second Largest Element
1 year ago
This probably isn't the most elegant way but it works :D I've returned the value but printed also it within the function.
void setup(){
noLoop();
}
void draw(){
int[] myArray = new int[10];
for(int i=0; i<10; i++){
myArray[i]=int(random(10));
}
get_second_max(myArray);
}
int get_second_max(int myArray[]){
int iMax=0;
int iMax2=0;
for(int i=0;i<10;i++){
if(myArray[i] >= iMax) iMax = myArray[i];
if((myArray[i] >= iMax2) && (myArray[i]!=iMax)) iMax2 =myArray[i];
println(myArray[i]);
}
println("Biggest = " + iMax);
println("Second Biggest = " + iMax2);
return(iMax2);
}
Leave a comment on not_55's reply
PhiLho
Re: How Would I Write a Function That Takes an Array as The Argument and Returns the Second Largest Element
11 months ago
I would have made similar code, but changing the loop to:
for (int i = 0; i < myArray.length; i++) {
to work with any array.
A little refinement would avoid checking for iMax2 if >= iMax.
Leave a comment on PhiLho's reply
PhiLho
Re: How Would I Write a Function That Takes an Array as The Argument and Returns the Second Largest Element
11 months ago
A better code:
void setup()
{
get_second_max(new int[] { 1 });
get_second_max(new int[] { 1, 2 });
get_second_max(new int[] { 1, 2, 3 });
get_second_max(new int[] { 1, 2, 3, 5 });
get_second_max(new int[] { 1, 2, 3, 5, 7 });
get_second_max(new int[] { -1, 2, -3, 5, -7 });
get_second_max(new int[] { 1, 2, 3, 5, 7, 5, 3, 2, 1 });
exit();
}
int get_second_max(int[] array)
{
int max1 = Integer.MIN_VALUE;
int max2 = Integer.MIN_VALUE;
for (int i = 0; i < array.length; i++)
{
if (array[i] > max1)
{
max2 = max1;
max1 = array[i];
}
else if (array[i] > max2)
{
max2 = array[i];
}
}
println("Biggest = " + max1);
println("Second Biggest = " + max2);
return max2;
}
Leave a comment on PhiLho's reply
Change topic type
Topic Type :
Discussions
Questions
No of days :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Change topic type
Cancel
Link this topic
Provide the permalink of a topic that is related to this topic
Permalink
Save
Close
Reply to joe po's question
Top
Reply
{"z4085862":[25080000001776063,25080000001776065],"z22255080":[25080000001773134],"z7188918":[25080000001773143]}
Statistics
3
Replies
345
Views
0
Followers
Tags
Cancel
array
Actions
Permalink
Related Posts
[Solved] Temporary variables
is ArrayList faster than array ?
calling on classes to recognize an ...
Array intersect question
Question regarding image arrays.