How do i get the highest number from the array.

Get help using Construct 2

Post » 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.

Who has the answer?
Thanx!

Kind Regards.
Savvy001
B
59
S
20
G
14
Posts: 779
Reputation: 13,857

Post » 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,878
Reputation: 19,562

Post » 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
91
S
31
G
103
Posts: 5,235
Reputation: 67,756

Post » 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,878
Reputation: 19,562

Post » Tue Jun 23, 2015 7:54 pm

Thanks Guys!!
That helped me out.
I Ninja'd the highest number ;-)
B
59
S
20
G
14
Posts: 779
Reputation: 13,857

Post » Tue Jun 23, 2015 11:16 pm

Should be:

for 0 to array.width-1
ImageImageImage
B
71
S
21
G
221
Posts: 3,649
Reputation: 124,227

Post » 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 :geek:
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,562

Post » Wed Jun 24, 2015 12:41 am

In this case, but as a general pattern, width-1 is correct.
ImageImageImage
B
71
S
21
G
221
Posts: 3,649
Reputation: 124,227

Post » Wed Jun 24, 2015 9:37 am

blackhornet is correct when he says general pattern, width-1 is correct.
B
245
S
39
G
15
Posts: 1,944
Reputation: 40,252


Return to How do I....?

Who is online

Users browsing this forum: el3um4s, imothep85, Ryan Kocourek, TheSupreme, TylerJS and 6 guests