# How do I Find lowest value in an Array and more

Get help using Construct 2

### » Sat Oct 01, 2016 6:21 am

Hey,

i was wondering how i should go about if i want to find the lowest value in an Array, for example, in Array X, Y, Z find the lowest Z first index value.

Second question is how do i go about finding the lowest neighbour in an Array. for example, once i found the lowest value, how do i search for the lowest neighbour for that lowest value, and then redo the process to find the lowest neighbour to that previously found lowest neighbour (without having the previous cell in the results).

I have a pretty good idea how to do so with lots of events but it seems rather inefficient and i was wondering if there's a quicker method...
B
16
S
6
G
1
Posts: 29
Reputation: 1,656

### » Sat Oct 01, 2016 6:48 am

You'll need 4 variables - lowestValue, Xindex, Yindex, and Zindex.

For each XYZ
Array.CurValue<lowestValue

Set lowestValue to Array.CurValue
Set Xindex to Array.CurX
Set Yindex to Array.CurY
Set Zindex to Array.CurZ

So it will go through the whole array, and whenever it finds a lower value than the last lowest value, it will record that new value and also the XYZ position.

With that first XYZ index, you can check the neighboring cells by adding or subtracting one from the appropriate index, with similar logic as before.

If you don't need the index/location of the lowest value, you can do a simple event as follows:
For each XYZ - Set lowestValue to min(Array.CurValue,lowestValue)

This might be useful if the value of interest is your first X axis - you can then use Array.IndexOf(lowestValue) to get the position. But that only works on the X axis.
Mistakes were made.
B
60
S
30
G
132
Posts: 1,930
Reputation: 74,337

### » Sat Oct 01, 2016 4:33 pm

Thanks! that's quite an efficient way to go about it!

Thanks again man, great help.
B
16
S
6
G
1
Posts: 29
Reputation: 1,656

Return to How do I....?

### Who is online

Users browsing this forum: R0J0hound, socialpilgrim and 6 guests