Trying to get the row number when the highest value met in an array

I am having issues with this:

I have and array that has a value in it that I want locate based on the highest value found starting from the highest row number. I need the row number to do the next action. Currently I get row 0 constantly.

the code for this section I have been working on is as follows:

for (int q = 34 ; q > -1 ; q–) { // this starts at 35 counts up to 0 for the highest urgency
if (topUrgency < MainArray[q] [2]) {
topUrgency = MainArray[q] [2]; // find highest urgency
topUrgencyRow = q;
}
}

does anyone have any idea why it is not stopping at the last highest value found but going all the way to 0

Thanks in advance

Is there a good reason to only look at the 3rd element in each row?
And, anyway, you need to show your array declaration.

boolrules:
And, anyway, you need to show your array declaration.

... and the rest of the code might be helpful.

Please remember to use code tags when posting code.

As the code is currently over 100 lines without notes I will not be posting it here.

the array structure is as follows:

const byte MRow = 35;
const byte MColumn = 6;

int MainArray [MRow] [MColumn] = {0};

I have a shuffle down sequence that fills the array from another source which is working correctly.

MColumn [2] is a value that is between 0 and 8.

What I'm looking for is a way to pick the value of MainArray [2] that is the highest starting at row 34 and going up.

In other words I need it to record the row that has the highest value starting at the first on.

so if I had:

1
5
8 I want it to record this row number and not continue and give me the value of 0
1
4
5
6

As the code is currently over 100 lines without notes I will not be posting it here

OK.

Please remember to use code tags when you eventually post code.

sorry that should have read 1000 lines without the cover notes

channe:
sorry that should have read 1000 lines without the cover notes

Please remember to use code tags when you eventually post code.

channe:
does anyone have any idea why it is not stopping at the last highest value found but going all the way to 0

if "it"is 'q' then "it" is going to zero because that's what the for loop says to do.
EDIT: 'q' actually goes to -1.
But you are saying that topUrgency and topUrgencyRow are not correct after the for loop ends?

How is topUrgency declared?

Did you remember to initialize topUrgency?

{
  topUrgencyRow = 34;
  topUrgency = MainArray[topUrgencyRow][2];
  for (int q = 33 ; q > -1 ; q--)   // this starts at 35 counts up to 0 for the highest urgency
  {
    if  (topUrgency < MainArray[q] [2])
    {
      topUrgency = MainArray[q] [2]; // find highest urgency
      topUrgencyRow = q;
    }
  }
}

Since you have to examine all of the entries to determine the maximum, why count backward? Counting upward is generally easier to read:

{
  topUrgencyRow = 0;
  topUrgency = MainArray[topUrgencyRow][2];
  for (int q = 1 ; q < 35 ; q++)
  {
    if  (topUrgency < MainArray[q][2])
    {
      topUrgency = MainArray[q][2]; // found a higher urgency
      topUrgencyRow = q;
    }
  }
}

int topUrgency ;
int topUrgencyRow ;

This is done in the same location as the array is declared

The idea is to get the first occurrence of the highest value so that is why I started at 35 and worked backwards. I know it sounds strange but on paper it is what is need for this project

I know it sounds strange

Yup.
Please remember to use code tags when you eventually decide to post code

channe:
The idea is to get the first occurrence of the highest value so that is why I started at 35 and worked backwards.

That would work if your comparison was

(topUrgency <= MainArray[q][2])

but your comparison is

(topUrgency < MainArray[q][2])

Your comparison works if you are starting at the beginning. If you WANT to start at the end and work backwards, change ‘<’ to ‘<=’.

AWOL:
Yup.
Please remember to use code tags when you eventually decide to post code

I think attaching code might be needed when it over 1000 lines :wink:

Yeah had tried that before as well as again now after a few other changes to no effect.

I might need to re look at this entire function again to see if there is another way of returning what I am wanting