How do i get the highest number from the array.

Get help using Construct 2

» Tue Jun 23, 2015 7:28 pm

Hi

I am trying to retrieve the highest number within a array.
lets say the array has stored 1 6 5 7 8 5 4 1 0 then i want the number 8.

Thanx!

Kind Regards.
Savvy001
B
59
S
21
G
14
Posts: 779
Reputation: 13,947

» Tue Jun 23, 2015 7:38 pm

use local var = 0

loop through the values in the array
if array value > local var set local var = array value

local var will have highest value from array
Who dares wins
B
57
S
17
G
21
Posts: 1,880
Reputation: 19,623

» Tue Jun 23, 2015 7:40 pm

ninja'd but here we go:

Code: Select all
`global number highest=0+----------------------+|                      | set highest to array.at(0)+----------------------+   +-------------------+   | array: for each x | set highest to max(highest, array.curValue)   +-------------------+`
B
98
S
36
G
132
Posts: 5,523
Reputation: 84,009

» Tue Jun 23, 2015 7:46 pm

R0J0hound wrote:ninja'd but here we go:

Code: Select all
`global number highest=0+----------------------+|                      | set highest to array.at(0)+----------------------+   +-------------------+   | array: for each x | set highest to max(highest, array.curValue)   +-------------------+`

fancy

I'm always fumbling with the build in array options :\ and tend to use:

var = 0

for 0 to array.width
(compare 2 values) array.at(loopindex) > var set var array.at(loopindex)
Who dares wins
B
57
S
17
G
21
Posts: 1,880
Reputation: 19,623

» Tue Jun 23, 2015 7:54 pm

Thanks Guys!!
That helped me out.
I Ninja'd the highest number
B
59
S
21
G
14
Posts: 779
Reputation: 13,947

» Tue Jun 23, 2015 11:16 pm

Should be:

for 0 to array.width-1
B
71
S
22
G
281
Posts: 3,842
Reputation: 154,886

» Wed Jun 24, 2015 12:28 am

blackhornet wrote:Should be:

for 0 to array.width-1

Redundant ... it would not have been > 0 anyways
Who dares wins
B
57
S
17
G
21
Posts: 1,880
Reputation: 19,623

» Wed Jun 24, 2015 12:41 am

In this case, but as a general pattern, width-1 is correct.
B
71
S
22
G
281
Posts: 3,842
Reputation: 154,886

» Wed Jun 24, 2015 9:37 am

blackhornet is correct when he says general pattern, width-1 is correct.
B
265
S
49
G
15
Posts: 2,028
Reputation: 42,733