 # Reading values from an array

Hey guys, after using the search function, I couldn’t find anything that answered my exact question.

I have a program which reads 5 inputs from pin 0 to pin 5 and stores them into an array. (I know that using pin 0 and pin 1 can lead to dodgy things, the real program won’t be using them, I just use them pins in this example for simplicity.)

For example, the pseudocode would look like:

``````for(readcounter = 0; readcounter<5; readcounter++){
}
``````

So for example, say the pins 1, 3 and 5 were high, my array would show:
inputvaluesarray = {1, 0, 1, 0, 1}

If say pins 1, 2, 4 were high, the array would show:
inputs values array = {1, 1, 0, 1, 0}

What I need to know is a piece of code which can say (using the first example) “The highest value in the array is inputvaluesarray”

Using the second example: “The highest value in the array is inputvaluesarray”

In code form, i’d like it to make a variable equal to the number in square brackets.

Using the first example, would show: “int highestarrayvalue = 5”
Using the second example, would show: “int highestarrayvalue = 4”

I hope that makes sense and anyone who can help will be appreciated greatly!

Thanks, Harris

What I need to know is a piece of code which can say (using the first example) "The highest value in the array is inputvaluesarray"

You probably don't. Such code would be wrong. There is no highest or lowest value when all the values are 0 or 1.

Please try again to explain what you want, remembering that array indices start at 0.

First off you can read this:-
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

What I need to know is a piece of code which can say (using the first example) “The highest value in the array is inputvaluesarray”

Do you mean that because the data will be a zero or a 1.
Or the highest array value with the highest number?

But simply search through the array and make a note of the highest value:-

``````highest = -99;
}
``````

Considering you are using digitalReads, you aren’t looking for the highest value in the array, you are looking for the highest index that contains a 1. So start at the highest index (4 for a 5 element array) and loop, decrementing until you find a 1. If you don’t find a 1, then your loop index will be -1, in which case you could handle it as such.

Hey guys, Sorry yeh, I think I need to re-explain this.

I have an array called inputvaluesary.

Say this array contains {1, 0, 0, 1, 0}

The first value is a 1, the second value is a zero, the third value is a zero, the fourth value is a 1 and the fifth value is zero.

What I need is a little snippet of code which will look through the array and tell me the number "highest" in the array (for example, in this case, I consider array higher than array)

In the above example the highest number would be the 4th entry (I know about the zero indexing so sorry about my stupid numbering)

So the program should look through that array, and say "ah! the 'highest' value is the 4th entry so I will make the variable highestvalue = 4"

Sorry for my utterly crap terminology but cheers for the replies!

Arrch: Considering you are using digitalReads, you aren't looking for the highest value in the array, you are looking for the highest index that contains a 1. So start at the highest index (4 for a 5 element array) and loop, decrementing until you find a 1. If you don't find a 1, then your loop index will be -1, in which case you could handle it as such.

Brilliant! Thats the kinda thing I need, could I do this using a for loop starting at 5, counting down to zero and within that for loop, add an if statement seeing whenever the array value==1?

Cheers,

In the above example the highest number would be the 4th entry (I know about the zero indexing so sorry about my stupid numbering)

I fail to see how the one in the 4th position is larger than the 1 in the 0th position.

could I do this using a for loop starting at 5

No. The highest index in a 5 element array is 4.

untested, and I probably spell cat wrong too

``````// Say this array contains {1, 0, 0, 1, 0}

byte bitArray = 0b01001; // low order bit is on the right here

byte highest = 16;
byte level = 5;

while ( highest ) // same as while ( highest > 0 )
{
if ( bitArray >= highest )  break; // exits the loop, your answer is in 'level'
highest /= 2;
level--;
}

Serial.print( "The highest is #" );
Serial.println( level, DEC );
``````

PaulS:

In the above example the highest number would be the 4th entry (I know about the zero indexing so sorry about my stupid numbering)

I fail to see how the one in the 4th position is larger than the 1 in the 0th position.

could I do this using a for loop starting at 5

No. The highest index in a 5 element array is 4.

This is what I meant by my crappy terminology, its just larger in the sense that its a higher index within the array, as I said:

myarray = {0, 1, 1, 0, 0, 1, 0, 1, 0, 0}

Looking at the above array, the largest value in there is 1 (1 is bigger than zero, I understand that) but i'm talking largest in the sense of which of the 1's has the highest index, in this case it will be the 4th '1' which has an index of 7 (its the 7th element in the array) and I want a little snippet of code that will tell me that the "highest" 1 is the 7th '1'.

Once again, I do apologise for my terrible explaining, its one of them moments where I know what I want to say, its just really hard to write it in words!

@GoForSmoke

How would I go about converting my array containing my values into a byte, as you have done in your code?

What should the answer be if none of the elements of the array or bits of the byte are set to 1 ?

How would I go about converting my array containing my values into a byte, as you have done in your code?

Something like this will find the highest element in the array that contains 1.

``````byte myarray = {0, 1, 1, 0, 0, 1, 0, 1, 0, 0};

byte highestOneIndex = -1;
for(byte i=sizeof(myarray)/sizeof(myarray)-1; i>=0; i--) // starting at 9, going down to 0...
{
if(myarray[i] == 1)
{
highestOneIndex = i;
break; // we're done
}
}
``````

PaulS:

How would I go about converting my array containing my values into a byte, as you have done in your code?

Something like this will find the highest element in the array that contains 1.

``````byte myarray = {0, 1, 1, 0, 0, 1, 0, 1, 0, 0};
``````

byte highestOneIndex = -1; for(byte i=sizeof(myarray)/sizeof(myarray)-1; i>=0; i--) // starting at 9, going down to 0... {    if(myarray[i] == 1)    {        highestOneIndex = i;        break; // we're done    } }

That would be absolutely perfect! By using "break;" does that prematurely end the for loop?

Thanks for that, I think that has solved my problem!

By using "break;" does that prematurely end the for loop?

That depends on your definition of prematurely. The loop is ended as soon as the first one is found, reading right to left. While that may be before the left end of the array is reached, it is not prematurely in my opinion.

PaulS:

By using "break;" does that prematurely end the for loop?

That depends on your definition of prematurely. The loop is ended as soon as the first one is found, reading right to left. While that may be before the left end of the array is reached, it is not prematurely in my opinion.

Sorry, I think prematurely is the wrong word and yes, before the left end of the array has been reached, perfect!

Thank you very much for the help, Harris

ooops! you found a bug, LOL!

It needs a bit more in case they're all zero. Maybe I spelled that kat?

Paul's version initialises the result to -1 and if no 1's are present it is unchanged and could be tested for. At least I assume that''s why he did it !

GoForSmoke: ooops! you found a bug, LOL!

Maybe I was wrong about being wrong....

highest: 16 8 4 2 1 -- exits on zero level: 5 4 3 2 1 -- and if 0 then none match

And all the pins can be read in ... if I have this correct, maybe 1 or 2 cycles.