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
21
G
14
Posts: 779
Reputation: 13,927

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,572

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
92
S
32
G
109
Posts: 5,290
Reputation: 70,991

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,572

Post » 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,927

Post » Tue Jun 23, 2015 11:16 pm

Should be:

for 0 to array.width-1
ImageImageImage
B
71
S
22
G
240
Posts: 3,732
Reputation: 133,990

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,572

Post » Wed Jun 24, 2015 12:41 am

In this case, but as a general pattern, width-1 is correct.
ImageImageImage
B
71
S
22
G
240
Posts: 3,732
Reputation: 133,990

Post » Wed Jun 24, 2015 9:37 am

blackhornet is correct when he says general pattern, width-1 is correct.
B
251
S
39
G
15
Posts: 1,964
Reputation: 40,721


Return to How do I....?

Who is online

Users browsing this forum: Blendi1223, kellehk, TRMG, Yahoo [Bot] and 28 guests