Go Down

Topic: How to get the SECOND highest value in a for() loop? (Read 34 times) previous topic - next topic


As this is the 'programming' forum, I hope this is appropriate. It is a purely language/function question. To put a finer point on the question, it has to be as simple/fast as possible (which I've already failed at below), as time in this loop is time not listening to the sensor, and I need to listen to it as much as possible to not 'miss' anything.

The code below is as good as I can get it for establishing a variable for the highest and 2nd highest value of a single variable in a for() loop:
Code: [Select]
// someArray[]... of ints...
int FirstHighestValue, SecondHighestValue;

for (uint16_t i = 0; i < sizeSomeArray; i++)
    // find highest value
    if (someArray[i] > FirstHighestValue) {
      FirstHighestValue = someArray[i];
    // find second highest
    if ((someArray[i] > SecondHighestValue) && (someArray[i] < FirstHighestValue) && (FirstHighestFreq != someArray[i])) {
      SecondHighestValue = someArray[i];
PM me regarding using technology for Stage/Close-up Magic!


Today at 05:10 pm Last Edit: Today at 05:11 pm by jremington
My approach would be to
1) search for the highest value and remember it
2) zero out that array entry
3) search again
4) etc. for the third highest


Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.


Code: [Select]
for (uint16_t i = o;
What is 'o' ?
Sorry, GV, that was supposed to be a zero(0)... fixed.
PM me regarding using technology for Stage/Close-up Magic!


How are the values getting into the array in the first place ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up